<?php
namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\AccountModel;
use app\index\model\ArchiveModel;
use app\index\model\BidModel;
use app\index\model\BookModel;
use app\index\model\CarModel;
use app\index\model\CertCompanyModel;
use app\index\model\CertEmployModel;
use app\index\model\CertModel;
use app\index\model\CertPersonModel;
use app\index\model\CertUseBidListModel;
use app\index\model\CertUseBidModel;
use app\index\model\CertUseListModel;
use app\index\model\CertUseModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiJieModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractChuJieModel;
use app\index\model\ContractChuListModel;
use app\index\model\ContractChuModel;
use app\index\model\ContractNeiListModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiGongModel;
use app\index\model\ContractShiJieModel;
use app\index\model\ContractShiModel;
use app\index\model\ContractYangModel;
use app\index\model\CossJieListModel;
use app\index\model\CossModel;
use app\index\model\DangJieListModel;
use app\index\model\DangModel;
use app\index\model\DealcomListModel;
use app\index\model\DealcomModel;
use app\index\model\DealcomZhengModel;
use app\index\model\DeptModel;
use app\index\model\DeviceJieListModel;
use app\index\model\DeviceJieModel;
use app\index\model\DeviceJinListModel;
use app\index\model\DeviceLinModel;
use app\index\model\DeviceTuiModel;
use app\index\model\DeviceYunModel;
use app\index\model\DossJieListModel;
use app\index\model\DossModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginBudshouModel;
use app\index\model\EnginBudsubModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginJianModel;
use app\index\model\EnginKuaModel;
use app\index\model\EnginModel;
use app\index\model\EnginSpeedListModel;
use app\index\model\EnginXunListModel;
use app\index\model\EnginXunModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\FlowNodeModel;
use app\index\model\FlowPrintModel;
use app\index\model\FlowPromerModel;
use app\index\model\FlowPromerOptModel;
use app\index\model\FlowShareModel;
use app\index\model\FlowTalkModel;
use app\index\model\FlowTaskModel;
use app\index\model\FlowTaskShenModel;
use app\index\model\FlowUsedModel;
use app\index\model\HouChujinGuanModel;
use app\index\model\HouChujinListModel;
use app\index\model\HouChushuiListModel;
use app\index\model\HouGonghuiListModel;
use app\index\model\HouGonghuiModel;
use app\index\model\HouGongshuiListModel;
use app\index\model\HouGongshuiModel;
use app\index\model\HouGongzhiListModel;
use app\index\model\HouJieModel;
use app\index\model\HouKouQifeiModel;
use app\index\model\HouKouShuifeiModel;
use app\index\model\HouTiaoModel;
use app\index\model\IncomeEnginModel;
use app\index\model\IncomeHuanModel;
use app\index\model\IncomeYushouModel;
use app\index\model\LabourBanModel;
use app\index\model\LabourGongModel;
use app\index\model\LabourHuaModel;
use app\index\model\LabourJieModel;
use app\index\model\LabourLingModel;
use app\index\model\LabourShenListModel;
use app\index\model\LabourShenModel;
use app\index\model\LabourXiangModel;
use app\index\model\LabourYongModel;
use app\index\model\OptionModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentChuModel;
use app\index\model\PaymentEnginListModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentGongModel;
use app\index\model\PaymentJieModel;
use app\index\model\PaymentNeifuModel;
use app\index\model\PaymentNeikouListModel;
use app\index\model\PaymentNeikouModel;
use app\index\model\PaymentYutuiModel;
use app\index\model\PaymentYuzhiModel;
use app\index\model\PengModel;
use app\index\model\PiaoChuListModel;
use app\index\model\PiaoChuModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoKouListModel;
use app\index\model\PiaoPutListModel;
use app\index\model\PiaoShouListModel;
use app\index\model\PiaoShouModel;
use app\index\model\PortAnModel;
use app\index\model\PortModel;
use app\index\model\QianShouModel;
use app\index\model\ReadsModel;
use app\index\model\SealModel;
use app\index\model\SpeedMonthListModel;
use app\index\model\SpeedMonthModel;
use app\index\model\SpeedZongListModel;
use app\index\model\SpeedZongModel;
use app\index\model\StuffCaiListModel;
use app\index\model\StuffCaiModel;
use app\index\model\StuffChuModel;
use app\index\model\StuffJieListModel;
use app\index\model\StuffJieModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\model\StuffTuiModel;
use app\index\model\SubbaoJieModel;
use app\index\model\SubbaoShenListModel;
use app\index\model\SubbaoShenModel;
use app\index\model\TodoModel;
use app\index\model\TouJiaoListModel;
use app\index\model\TouJiaoModel;
use app\index\model\UserGangModel;
use app\index\model\UserModel;
use app\index\model\VisaModel;
use app\index\model\WenBanModel;
use app\index\model\WenFaModel;
use app\index\model\WenShouModel;
use app\index\model\WuCaiListModel;
use app\index\model\WuCaiModel;
use app\index\model\WuChuModel;
use app\index\model\WuLingListModel;
use app\index\model\WuRuListModel;
use app\index\model\XossJieListModel;
use app\index\model\XossModel;
use app\index\service\BidService;
use app\index\service\BudgetService;
use app\index\service\CertService;
use app\index\service\ContractService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\HouService;
use app\index\service\LabourService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\PaymentService;
use app\index\service\PiaoService;
use app\index\service\QianService;
use app\index\service\UserService;
use app\index\service\XingService;
use think\Db;
use think\Request;

class Opt extends Common
{

    public function get_readsandlog(Request $request){
        $param = $request->post();
        if(!isset($param['table']) || empty($param['table'])){
            return outputJson(2000, lang('code_2000'), []);
        }
        if(!isset($param['mid']) || empty($param['mid'])){
            return outputJson(2000, lang('code_2000'), []);
        }

        $shuju = Db::name($param['table'])->where(['id'=>$param['mid']])->find();
        if(!$shuju){
            return outputJson(2000, lang('code_2000'), []);
        }

        $dataDelStr = '';
        if(isset($shuju['deleted_user']) && $shuju['deleted_user'] > 0){
            $delUser = UserModel::where(['id'=>$shuju['deleted_user']])->value('realname');
            $delTime = '';
            if($shuju['deleted_at'] && $shuju['deleted_at'] != '0000-00-00 00:00:00'){
                $delTime = date("Y年m月d日 H时i分", strtotime($shuju['deleted_at']));
            }
            $dataDelStr = "当前数据已被【".$delUser." ".$delTime."】删除";
        }



        try {
            TodoModel::where(['uid'=>$this->userinfo['id'],'table'=>$param['table'],'mid'=>$param['mid'],'status'=>0])->update([
                'status'=>1,'readdt'=>date("Y-m-d H:i:s")
            ]);

            $flowlog = FlowLogModel::where(['table'=>$param['table'],'mid'=>$param['mid']])->select()->toArray();
            foreach ($flowlog as &$item){
                $user = UserModel::field('realname,avatar')->where(['id'=>$item['created_user']])->find();
                $item['created_user'] = $user['realname'];
                $item['avatar'] = $user['avatar'];
            }

            $readsModel = new ReadsModel();
            $info = $readsModel->where(['table'=>$param['table'],'mid'=>$param['mid'],'optid'=>$this->userinfo['id']])->find();
            if($info){
                $info->optdt = date('Y-m-d H:i:s');
                $info->stotal = $info['stotal'] + 1;
                $info->save();
            }else{
                $readsModel->add_reads($param['table'], $param['mid'], $this->userinfo['id'],$this->userinfo['comid']);
            }
            $reads = $readsModel->alias('r')->field('r.stotal,r.optdt,u.realname,u.avatar')
                ->join('user u','u.id=r.optid')
                ->where(['table'=>$param['table'],'mid' => $param['mid']])->select()->toArray();
        } catch (\Exception $e) {
            return outputJson(2000, lang('code_2000'), []);
        }
        return outputJson(2000, lang('code_2000'), [
            'dataDelStr' => $dataDelStr,
            'flowlog' => $flowlog,
            'reads' => $reads,
        ]);
    }

    public function get_flowprint(Request $request){
        $param = $request->post();
        if(!isset($param['table']) || empty($param['table'])){
            return outputJson(2000, lang('code_2000'), []);
        }
        if(!isset($param['mid']) || empty($param['mid'])){
            return outputJson(2000, lang('code_2000'), []);
        }


        $info = FlowPrintModel::where(['table'=>$param['table'],'mid'=>$param['mid']])->find();
        $nums = 0;
        if($info){
            $nums = $info['nums'];
        }else{
            FlowPrintModel::insert([
                'table'=>$param['table'],
                'mid'=>$param['mid'],
                'nums'=>$nums,
            ]);
        }

        $data['nums'] = $nums;
        return outputJson(2000, lang('code_2000'), $data);
    }

    public function add_flowprint(Request $request){
        $param = $request->post();
        if(!isset($param['table']) || empty($param['table'])){
            return outputJson(2000, lang('code_2000'), []);
        }
        if(!isset($param['mid']) || empty($param['mid'])){
            return outputJson(2000, lang('code_2000'), []);
        }

        $nums = 0;
        $info = FlowPrintModel::where(['table'=>$param['table'],'mid'=>$param['mid']])->find();
        if($info){
            $nums = $info['nums'];
        }else{
            FlowPrintModel::insert([
                'table'=>$param['table'],
                'mid'=>$param['mid'],
                'nums'=>$nums,
            ]);
        }
        $nums++;

        Db::startTrans();
        try {
            FlowPrintModel::where(['table'=>$param['table'], 'mid'=>$param['mid']])->update(['nums'=>$nums]);

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }

    public function get_flowtalk(Request $request){
        $param = $request->post();
        $data = FlowTalkModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$param['id']])->select()->toArray();
        foreach ($data as &$item){
            $item['domer_name'] = UserModel::get_realname_by_ids($item['domer']);

            $uidinfo = UserModel::field('realname,avatar')->where(['id'=>$item['uid']])->find();
            $item['uid_name'] = $uidinfo['realname'];
            $item['uid_avatar'] = $uidinfo['avatar'];

            $item['filesarr'] = FileModel::field('id,table,mid,name,ext,size,encpath,fileplain,created_user,created_at')
                ->where('id','in',$item['files'])->select()->toArray();
        }
        return outputJson(2000, lang('code_2000'), $data);
    }


    public function get_userpart(Request $request){
        $keyword = $request->param('keyword', '', 'string');
        $type = $request->param('type', 'user', 'string');
        $dept = $request->param('dept', '', 'string');

        $where = [];
        if($dept == 'chan'){
            $where[] = ['type', '=', 2];
        }
        if($type == 'dept'){
            $where[] = ['name','like','%'.$keyword.'%'];
        }

        $whereUser = [];
        if($type == 'user'){
            $whereUser[] = ['realname','like','%'.$keyword.'%'];
        }
        $data = DeptModel::where(['status'=>1,'deleted_user'=>0])->where($where)->field('id,pid,name as title,sort')->select()->toArray();
        foreach ($data as &$item){
            $item['basicData'] = ['id'=>$item['id'],'name'=>$item['title'],'part'=>'dept'];
            $item['children'] = [];

            if($type == 'user' || $type == 'part'){
                $users = UserModel::where(['deleted_user'=>0,'status'=>1,'deptid'=>$item['id']])->where($whereUser)
                    ->field('id,realname as title')->select()->toArray();
                foreach ($users as &$user){
                    $user['sort'] = 0;
                    $user['basicData'] = ['id'=>$user['id'],'name'=>$user['title'],'part'=>'user'];
                    $user['name'] = $user['title'];
                    $user['title'] = '&nbsp;&nbsp;<i class="layui-icon layui-icon-username"></i>&nbsp;&nbsp;'.$user['title'];
                }
                $item['children'] = $users;
            }

            if(count($item['children']) > 0){
                $item['title'] = '<i class="layui-icon layui-icon-template"></i>&nbsp;&nbsp;'.$item['title'];
            }else{
                $item['title'] = '&nbsp;&nbsp;<i class="layui-icon layui-icon-template"></i>&nbsp;&nbsp;'.$item['title'];
            }

        }



        $data = array2tree($data, 'id', 'pid','children','sort');
        return json_encode([
            'status'=>['code'=>200,'message'=>"操作成功"],
            'data'=>$data,
            'code'=>2000,
            'msg'=>'请求成功',
        ]);
    }



    // 部门人员
    public function get_dept_user(){
        $input = input('post.');
        $where = [];
        if(isset($input['keyword']) && !empty($input['keyword'])){
            $where[] = ['realname','like','%'.$input['keyword'].'%'];
        }

        $data = DeptModel::where(['status'=>1,'deleted_user'=>0])->field('id,pid,name as title')->select()->toArray();
        foreach ($data as &$item){
            $rawWhere = Db::raw("find_in_set(".$item['id'].",deptid)");
            $users = UserModel::where($rawWhere)->where(['deleted_user'=>0,'status'=>1])->where($where)->field('id,realname as title')->select()->toArray();
            foreach ($users as &$user){
                $user['name'] = $user['title'];
                $user['title'] = '&nbsp;&nbsp;<i class="layui-icon layui-icon-username"></i>&nbsp;&nbsp;'.$user['title'];
            }
            $item['children'] = $users;

            if(count($item['children']) > 0){
                if(isset($input['keyword']) && !empty($input['keyword'])){
                    $item['spread'] = true;
                }
                $item['title'] = '<i class="layui-icon layui-icon-template"></i>&nbsp;&nbsp;'.$item['title'];
            }else{
                $item['title'] = '&nbsp;&nbsp;<i class="layui-icon layui-icon-template"></i>&nbsp;&nbsp;'.$item['title'];
            }
        }

        $data = array2tree($data);
        return outputJson(2000,lang('code_2000'),$data);
    }

    // 部门
    public function get_dept(){
        $input = input('post.');
        $where = [];
        if(isset($input['keyword']) && !empty($input['keyword'])){
            $where[] = ['name','like','%'.$input['keyword'].'%'];
        }
        $data = DeptModel::where(['status'=>1,'deleted_user'=>0])->where($where)->field('id,pid,name')->select()->toArray();
        foreach ($data as &$item){
            $item['title'] = $item['name'];
//            $item['title'] = '<i class="layui-icon layui-icon-template"></i>&nbsp;&nbsp;'.$item['name'];
        }
        $data = array2tree($data);
        return outputJson(2000,lang('code_2000'),$data);

    }

    // 证书
    public function get_cert(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cert.deleted_user','=','0'];
        $where[] = ['cert.comid','=',$this->userinfo['comid']];
        $where[] = ['cert.state','not in', [2,3]];



        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['person.name|cert.name|cert.number|cert.major|cert.issue_unit|u.realname', '%'.$param['keyword'].'%'];
        }


        if(isset($param['statueuse']) && !empty($param['statueuse'])){
            $where[] = ['cert.statueuse','=',$param['statueuse']];
        }
        if(isset($param['person_nature']) && !empty($param['person_nature'])){
            $where[] = ['person.nature','=',$param['person_nature']];
        }
        if(isset($param['person_name']) && !empty($param['person_name'])){
            $where[] = ['person.name','like', '%'.$param['person_name'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['cert.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cert.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['major']) && !empty($param['major'])){
            $where[] = ['cert.major','like', '%'.$param['major'].'%'];
        }
        if(isset($param['issue_unit']) && !empty($param['issue_unit'])){
            $where[] = ['cert.issue_unit','like', '%'.$param['issue_unit'].'%'];
        }
        if(isset($param['issue_dt']) && !empty($param['issue_dt'])){
            $where[] = ['cert.issue_dt','between', explode(' - ', $param['issue_dt'])];
        }
        if(isset($param['expiry_dt']) && !empty($param['expiry_dt'])){
            $expiry_dt = explode(' - ', $param['expiry_dt']);
            $ids = [];
            $c_data = CertModel::where(['deleted_user'=>0])->field('id,expiry_dt')->select();
            foreach ($c_data as $item){
                $expiry_dtArr = explode('[]',$item['expiry_dt']);
                if($expiry_dtArr){
                    foreach ($expiry_dtArr as $dt){
                        if($dt >= $expiry_dt[0] && $dt <= $expiry_dt[1]){
                            $ids[] = $item['id'];
                        }
                    }
                }
            }
            $ids = array_unique($ids);
            $where[] = ['cert.id','in',$ids];
        }
        if(isset($param['form']) && !empty($param['form'])){
            $where[] = ['cert.form','eq', $param['form']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['cert.state','in', $param['state']];
        }
        if(isset($param['electron_dt']) && !empty($param['electron_dt'])){
            $where[] = ['cert.electron_dt','between', explode(' - ', $param['electron_dt'])];

        }
        if(isset($param['inspect_dt']) && !empty($param['inspect_dt'])){
            $where[] = ['cert.inspect_dt','between', explode(' - ', $param['inspect_dt'])];
        }
        if(isset($param['custodianid']) && !empty($param['custodianid'])){
            $where[] = ['u.realname','like', '%'.$param['custodianid'].'%'];
        }
        if(isset($param['store_addr']) && !empty($param['store_addr'])){
            $where[] = ['cert.store_addr','eq', $param['store_addr']];
        }

        if(isset($param['goes']) && !empty($param['goes'])){
            $where[] = ['cert.goes','=', $param['goes']];
        }
        if(isset($param['use']) && !empty($param['use'])){
            $where[] = ['cert.use','=', $param['use']];
        }
        if(isset($param['person_state']) && !empty($param['person_state'])){
            $where[] = ['person.state','=', $param['person_state']];
        }
        if(isset($param['cout_dt']) && !empty($param['cout_dt'])){
            $where[] = ['cert.cout_dt','between', explode(' - ', $param['cout_dt'])];
        }
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['cert.type','=', $param['type']];
        }
        if(isset($param['personid']) && !empty($param['personid'])){
            $where[] = ['cert.personid','=',$param['personid']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['form'] = (new CertService())->get_cert_form();
        $data['is_form'] = isset($param['form']) ? $param['form'] : '';

        $data['state'] = (new CertService())->get_cert_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['statueuse'] = (new CertService())->get_cert_statueuse();
        $data['is_statueuse'] = isset($param['statueuse']) ? $param['statueuse'] : '';

        $data['goes'] = (new CertService())->get_cert_goes();
        $data['is_goes'] = isset($param['goes']) ? $param['goes'] : '';

        $data['nature'] = (new OptService())->get_base_info('person_nature');
        $data['is_nature'] = isset($param['nature']) ? $param['nature'] : '';

        $data['store_addr'] = (new OptService())->get_base_info('cert_store_addr');
        $data['is_store_addr'] = isset($param['store_addr']) ? $param['store_addr'] : '';



        if(isset($param['isborrow']) && !empty($param['isborrow'])){
            if($param['isborrow'] == 'borrow'){
                $where[] = ['cert.state','=',1];
                $where[] = ['cert.goes','=',1];
                $where[] = ['cert.form','=',1];
            }
            if($param['isborrow'] == 'down'){
                $where[] = ['cert.state','=',1];
            }
        }



        $data['data'] = CertModel::alias('cert')
            ->leftjoin('cert_person person','person.id=cert.personid')
            ->leftJoin('user u','u.id = cert.custodianid')
            ->field('cert.*,u.realname as custodian_name,person.name as person_name,person.state as person_state,
            person.nature as person_nature,person.statueuse as person_statueuse')
            ->where($where)
            ->where((new MenuService())->get_opt_where('cert', 'cert'))
            ->order('cert.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['form_name'] = (new CertService())->get_cert_form_name($item['form']);
                $item['state_name'] = (new CertService())->get_cert_state_name($item['state']);
                $item['person_statueuse_name'] = (new CertService())->get_cert_person_statueuse_name($item['person_statueuse']);
                $item['statueuse_name'] = (new CertService())->get_cert_statueuse_name($item['statueuse']);
                $item['goes_name'] = (new CertService())->get_cert_goes_name($item['goes']);
                $item['person_nature'] = (new OptService())->get_base_showname('person_nature',$item['person_nature']);
                $item['expiry_dt_name'] = str_replace('[]','，',$item['expiry_dt']);
                $item['major_name'] = str_replace('[]','，',$item['major']);
                $major = array_filter(explode('[]',$item['major']));
                $item['major_str'] = implode(' / ',$major);
                $item['major'] = '<div style="line-height: 20px">'.str_replace('[]','<br/>',$item['major']).'</div>';

                $expiry_dtArr = explode('[]',$item['expiry_dt']);
                $arr = [];
                foreach($expiry_dtArr as $v){
                    if(empty($v)){
                        $arr[] = $v;
                    }else{
                        if($v < date('Y-m-d')){
                            $arr[] = '<span style="color:red;">'.$v.'</span>';
                        }else{
                            $arr[] = $v;
                        }
                    }
                }
                $item['expiry_dt'] = '<div style="line-height: 20px">'.implode('<br/>',$arr).'</div>';

                $item['custodian_name'] = UserModel::get_realname_by_pk($item['custodianid']);
                $item['type'] = (new OptService())->get_base_showname('cert_type', $item['type']);
                $item['store_addr'] = (new OptService())->get_base_showname('cert_store_addr',$item['store_addr']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    // 证书-用证明细
    public function get_cert_use_list()
    {
        $input = input('post.');
        get_page($input);

        $where[] = ['list.pid','eq','0'];
        $where[] = ['list.isdel','eq','0'];
        $where[] = ['use.deleted_user','eq','0'];
        $where[] = ['use.comid','eq',$this->userinfo['comid']];
        $where[] = ['use.status','eq','1'];

        if(isset($input['status']) && !empty($input['status'])){
            $where[] = ['cert.status','eq', $input['status']];
        }
        if(isset($input['goes']) && !empty($input['goes'])){
            $where[] = ['cert.goes','eq', $input['goes']];
        }

        $form = CertModel::get_form();
        $status = CertModel::get_status();
        $goes = CertModel::get_goes();
        $use = CertModel::get_use();
        $data['data'] = CertUseListModel::alias('list')->field('cert.*,person.name as person_name,person.nature as person_nature,person.state as person_state')
            ->leftJoin('cert_use use','use.id = list.mid')
            ->leftJoin('cert','cert.id = list.cert_id')
            ->leftJoin('cert_person person','person.id = cert.personid')
            ->where($where)
            // ->where(function ($q){
            //     $q->where('use.billid',0)->whereOr([['use.billid','>','0'],['use.status','eq','1']]);
            // })
            ->order('list.id desc')
            ->paginate($input['pagesize'], false, ['page'=>$input['page']])
            ->each(function ($item,$key)use($form,$status,$goes,$use){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['person_nature'] = (new OptService())->get_base_showname('person_nature',$item['person_nature']);
                $item['form_name'] = isset($form[$item['form']]) ? $form[$item['form']] : '';
                $item['status_name'] = isset($status[$item['status']]) ? $status[$item['status']] : '';
                $item['major_name'] = str_replace('[]','，',$item['major']);
                $item['expiry_dt'] = str_replace('[]','，',$item['expiry_dt']);
                $item['com_name'] = DeptModel::get_name_by_pk($item['chanid']);
                if(empty($item['com_name'])){
                    $item['com_name'] = '';
                }
                // 用证情况
                $item['use_name'] = isset($use[$item['use']]) ? $use[$item['use']] : '';
                $item['goes_name'] = isset($goes[$item['goes']]) ? $goes[$item['goes']] : '';
            })->toArray();

        $data['type'] = (new OptService())->get_base_info('cert_type');
        $data['store_addr'] = (new OptService())->get_base_info('cert_store_addr');
        $data['form'] = $form;
        $data['status'] = $status;
        $data['goes'] = $goes;
        $data['use'] = $use;
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['person_nature'] = (new OptService())->get_base_info('person_nature');
        $data['person_status'] = CertPersonModel::get_status();
        $data['issuo'] = CertUseModel::get_issuo();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_cert_use(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['use.deleted_user','=','0'];
        $where[] = ['use.comid','=',$this->userinfo['comid']];
        $where[] = ['use.isturn','=',1];
        $where[] = ['use.status','=',1];
        $where[] = ['use.isreal','=',1];
        $where[] = ['use.created_user','=',$this->userinfo['id']];

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['use.chanid','=', $param['chanid']];
        }
        if(isset($param['created_user']) && $param['created_user'] != ''){
            $where[] = ['u.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $end_at = $param['created_at'].' 23:59:59';
            $where[] = ['use.created_at','between', [$param['created_at'],$end_at]];
        }
        if(isset($param['enginid']) && $param['enginid'] != ''){
            $where[] = ['use.enginid','like', '%'.$param['enginid'].'%'];
        }
        if(isset($param['dt']) && $param['dt'] != ''){
            $where[] = ['use.dt','=', $param['dt']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['use.status','=', $param['status']];
        }

        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['use.type','=',$param['type']];
        }


        $missName = '';
        if($param['type'] == 1){
            $missName = 'cert_use_bid';
        }else{
            $missName = 'cert_use';
        }



        $type = CertUseModel::get_type();
        $apply_type = CertUseModel::get_apply_type();
        $data['data'] = CertUseModel::alias('use')
            ->leftJoin('engin e','e.id = use.enginid')
            ->leftJoin('bid b','b.id = use.enginid')
            ->leftJoin('user u','u.id = use.created_user')
            ->field('use.*,e.name as engin_name,b.name as bid_name')
            ->where($where)
            ->where((new MenuService())->get_miss_where($missName, 'use'))
            ->order('use.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use ($type,$apply_type){
                if($item['type'] == 1){
                    $item['engin_name'] = $item['bid_name'];
                }

                // 用证数量 首次只查询首次，变更查询变更后的（包含审批中的）
                if($item['apply_type'] == 1){ // 首次
                    $item['num'] = CertUseListModel::where(['mid'=>$item['id']])->count();
                }else if($item['apply_type'] == 2){ // 变更
                    if($item['status'] == 0){ // 审批中的数据
                        // 查询项目下所有的在用证书，去除大于当前id，去除删除的证书，去除变更的证书
                        $delIds = CertUseListModel::where(['mid'=>$item['id']])->where('temp_isdel','>',0)->column('temp_isdel');
                        $pidIds = CertUseListModel::where(['mid'=>$item['id']])->where('temp_pid','>',0)->column('temp_pid');

                        $ids = CertUseModel::where(['enginid'=>$item['enginid'],'deleted_user'=>0,'isturn'=>1])
                            ->where('status','<>',2)->where('id','<=',$item['id'])->column('id');

                        $item['num'] = CertUseListModel::where('mid','in',$ids)->where('id','not in',$delIds)
                            ->where('id','not in',$pidIds)->where(['temp_isdel'=>0,'pid'=>0,'isdel'=>0])->count();
                    }else if($item['status'] == 1){
                        $delIds = CertUseListModel::where(['mid'=>$item['id']])->where('temp_isdel','>',0)->column('temp_isdel');
                        $pidIds = CertUseListModel::where(['mid'=>$item['id']])->where('temp_pid','>',0)->column('temp_pid');

                        $ids = CertUseModel::where(['enginid'=>$item['enginid'],'deleted_user'=>0,'isturn'=>1])
                            ->where('status','<>',2)->where('id','<',$item['id'])->column('id');

                        $item['num'] = CertUseListModel::where('mid','in',$ids)->where('id','not in',$delIds)
                            ->where('id','not in',$pidIds)->where(['temp_isdel'=>0,'pid'=>0,'isdel'=>0])->count();

                        $item['num'] += CertUseListModel::where('mid','in',$item['id'])->where(['temp_isdel'=>0])->count();
                    }
                }

                $item['type_name'] = isset($type[$item['type']]) ? $type[$item['type']] : '';
                $item['apply_type_name'] = isset($apply_type[$item['apply_type']]) ? $apply_type[$item['apply_type']] : '';
                if($item['billid'] == 0){
                    $item['flowuser'] = UserModel::get_realname_by_pk($item['created_user']);
                }
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('cert_use');
        $data['type'] = $type;
        $data['apply_type'] = $apply_type;
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        return outputJson(2000,lang('code_2000'),$data);
    }


    // 企业
    public function get_cert_company()
    {
        $param = input('post.');
        get_page($param);

        $where[] = ['cert.deleted_user','=','0'];
        $where[] = ['cert.pid','=','0'];
        $where[] = ['cert.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cert.name|cert.number|cert.issue_unit|cert.zinumber|u.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['cert.chanid','=', $param['chanid']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['cert.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cert.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['form']) && !empty($param['form'])){
            $where[] = ['cert.form','eq', $param['form']];
        }
        if(isset($param['goes']) && !empty($param['goes'])){
            $where[] = ['cert.goes','=', $param['goes']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['cert.state','eq', $param['state']];
        }
        if(isset($param['issue_unit']) && !empty($param['issue_unit'])){
            $where[] = ['cert.issue_unit','like', '%'.$param['issue_unit'].'%'];
        }
        if(isset($param['issue_dt']) && !empty($param['issue_dt'])){
            $where[] = ['cert.issue_dt','=', $param['issue_dt']];
        }
        if(isset($param['expiry_dt']) && !empty($param['expiry_dt'])){
            $where[] = ['cert.expiry_dt','like', '%'.$param['expiry_dt'].'%'];
        }
        if(isset($param['inspect_dt']) && !empty($param['inspect_dt'])){
            $where[] = ['cert.inspect_dt','=', $param['inspect_dt']];
        }
        if(isset($param['zinumber']) && !empty($param['zinumber'])){
            $where[] = ['cert.zinumber','like', '%'.$param['zinumber'].'%'];
        }
        if(isset($param['custodianid']) && !empty($param['custodianid'])){
            $where[] = ['u.realname','like', '%'.$param['custodianid'].'%'];
        }
        if(isset($param['store_addr']) && !empty($param['store_addr'])){
            $where[] = ['cert.store_addr','eq', $param['store_addr']];
        }

        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['cert.type','eq', $param['type']];
        }


        if(isset($param['isborrow']) && !empty($param['isborrow'])){
            if($param['isborrow'] == 'borrow'){
                $where[] = ['cert.state','=',1];
                $where[] = ['cert.goes','=',1];
                $where[] = ['cert.form','=',1];
            }
            if($param['isborrow'] == 'down'){
                $where[] = ['cert.state','=',1];
            }
        }


        $form = (new CertService())->get_cert_company_form();
        $state = (new CertService())->get_cert_company_state();
        $goes = (new CertService())->get_cert_company_goes();
        $data['data'] = CertcompanyModel::alias('cert')
            ->leftJoin('user u','u.id = cert.custodianid')
            ->field('cert.*,u.realname as custodian_name')
            ->where($where)
            ->order('cert.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['form_name'] = (new CertService())->get_cert_company_form_name($item['form']);
                $item['state_name'] = (new CertService())->get_cert_company_state_name($item['state']);
                $item['goes_name'] = (new CertService())->get_cert_company_goes_name($item['goes']);
                $item['custodian_name'] = UserModel::get_realname_by_pk($item['custodianid']);
                $item['chan_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['files'] = (new OptService())->get_file('cert', $item['id']);
                $item['type'] = (new OptService())->get_base_showname('cert_company_type', $item['type']);
                $item['store_addr'] = (new OptService())->get_base_showname('cert_store_addr', $item['store_addr']);
                return $item;
            })->toArray();

        $old_data = $data['data']['data'];
        $now_data = [];
        foreach ($old_data as $item){
            $now_data[] = $item;
            $children = CertcompanyModel::where(['deleted_user'=>0,'pid'=>$item['id']])->select()->toArray();
            foreach ($children as $v){
                $v['form_name'] = (new CertService())->get_cert_company_form_name($v['form']);
                $v['state_name'] = (new CertService())->get_cert_company_state_name($v['state']);
                $v['goes_name'] = (new CertService())->get_cert_company_goes_name($v['goes']);
                $v['custodian_name'] = UserModel::get_realname_by_pk($v['custodianid']);
                $v['chan_name'] = DeptModel::get_name_by_pk($v['chanid']);
                $v['files'] = (new OptService())->get_file('cert', $v['id']);
                $v['type'] = (new OptService())->get_base_showname('cert_company_type', $v['type']);
                $v['store_addr'] = (new OptService())->get_base_showname('cert_store_addr', $v['store_addr']);
                $now_data[] = $v;
            }
        }
        $data['data']['data'] = $now_data;

        $data['butauth'] = (new MenuService())->get_butauth('cert_company');
        $data['type'] = (new OptService())->get_base_info('cert_company_type');
        $data['store_addr'] = (new OptService())->get_base_info('cert_store_addr');
        $data['form'] = $form;
        $data['state'] = $state;
        $data['goes'] = $goes;
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        return outputJson(2000,lang('code_2000'),$data);
    }

    // 人员
    public function get_cert_person(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['p.deleted_user','=','0'];
        $where[] = ['p.comid','=',$this->userinfo['comid']];



        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['p.name|p.idcard|p.tel|p.socialareas','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['p.chanid','=', $param['chanid']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['p.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['idcard']) && !empty($param['idcard'])){
            $where[] = ['p.idcard','like', '%'.$param['idcard'].'%'];
        }
        if(isset($param['sex']) && !empty($param['sex'])){
            $where[] = ['p.sex','=', $param['sex']];
        }
        if(isset($param['tel']) && !empty($param['tel'])){
            $where[] = ['p.tel','like', '%'.$param['tel'].'%'];
        }
        if(isset($param['nature']) && !empty($param['nature'])){
            $where[] = ['p.nature','=',$param['nature']];
        }
        if(isset($param['intodt']) && !empty($param['intodt'])){
            $where[] = ['p.intodt','between', explode(' - ', $param['intodt'])];
        }
        if(isset($param['social']) && !empty($param['social'])){
            $where[] = ['p.social','=', $param['social']];
        }
        if(isset($param['socialareas']) && !empty($param['socialareas'])){
            $where[] = ['p.socialareas','like', '%'.$param['socialareas'].'%'];
        }
        if(isset($param['socstartdt']) && !empty($param['socstartdt'])){
            $where[] = ['p.socstartdt','between', explode(' - ', $param['socstartdt'])];
        }
        if(isset($param['outdt']) && !empty($param['outdt'])){
            $where[] = ['p.outdt','between', explode(' - ', $param['outdt'])];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['p.state','=', $param['state']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['nature'] = (new OptService())->get_base_info('person_nature');
        $data['is_nature'] = isset($param['nature']) ? $param['nature'] : '';

        $data['social'] = (new OptService())->get_base_info('person_social');
        $data['is_social'] = isset($param['social']) ? $param['social'] : '';

        $data['statueuse'] = (new CertService())->get_cert_person_statueuse();
        $data['is_statueuse'] = isset($param['statueuse']) ? $param['statueuse'] : '';

        $wherePage = [];
        if($this->userinfo['token_roletype'] != 1){
            if(isset($param['parentpage']) && $param['parentpage'] == 'cert'){
                $userChanid = (new UserService())->get_user_chanid();
                $chanid_son = (new UserService())->get_chansonid($userChanid);
                $wherePage[] = ['p.chanid','in', $chanid_son];
            }
        }


        $data['data'] = CertPersonModel::alias('p')
            ->field('p.*,u.realname as created_user_name')
            ->leftjoin('user u','u.id=p.created_user')
            ->where($where)
            ->where($wherePage)
            ->where((new MenuService())->get_opt_where('cert_person', 'p'))
            ->order('p.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['age'] = (new CertService())->get_age_by_idcard($item['idcard']);
                $item['statueuse_name'] = (new CertService())->get_cert_person_statueuse_name($item['statueuse']);
                $item['cert_num'] = CertModel::where(['personid'=>$item['id'],'deleted_user'=>0])->count();
                $item['social_name'] = (new OptService())->get_base_showname('person_social',$item['social']);
                $item['xueli_name'] = (new OptService())->get_base_showname('person_xueli',$item['xueli']);
                $item['nature_name'] = (new OptService())->get_base_showname('person_nature',$item['nature']);
                $item['socnum'] = (new CertService())->get_cert_person_social_month($item['socstartdt'],$item['socstartdt']);
                $item['deptid_name'] = DeptModel::get_name_by_pk($item['deptid']);
                $item['gangid_name'] = UserGangModel::get_name_by_ids($item['gangid'],' / ');
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }





    public function get_hou_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jie.chuer|jie.jieer|jier.daner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['chuer']) && !empty($param['chuer'])){
            $where[] = ['jie.chuer','like', '%'.$param['chuer'].'%'];
        }
        if(isset($param['jieer']) && !empty($param['jieer'])){
            $where[] = ['jie.jieer','like', '%'.$param['jieer'].'%'];
        }
        if(isset($param['daner']) && !empty($param['daner'])){
            $where[] = ['jie.daner','like', '%'.$param['daner'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['jie.state','=', $param['state']];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['jie.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }

        $data['state'] = (new ContractService())->get_contract_cai_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = HouJieModel::alias('jie')
            ->field('jie.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('hou_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['fangtype_name'] = (new OptService())->get_base_showname('cai_jie_fangtype', $item['fangtype']);
                $item['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($item['id'], $item['state']);

                //放款合计金额
                $item['jiemoney'] = HouJieModel::where(['id'=>$item['id']])->value('money');
                $item['fangfee'] = HouTiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])
                    ->where(['jieid'=>$item['id']])->sum('money');
                $item['daifee'] = round($item['jiemoney'] - $item['fangfee'], 2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_peng(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['peng.deleted_user','=', 0];
        $where[] = ['peng.comid','=',$this->userinfo['comid']];
        $where[] = ['peng.isturn','=',1];
        $where[] = ['peng.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['peng.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['peng.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['gouxing']) && $param['gouxing'] != ''){
            $where[] = ['peng.gouxing','=', $param['gouxing']];
        }
        if(isset($param['goudt']) && !empty($param['goudt'])){
            $where[] = ['peng.goudt','between', explode(' - ', $param['goudt'])];
        }


        $data['gouxing'] = (new OptService())->get_base_info('peng_gouxing');
        $data['is_gouxing'] = isset($param['gouxing']) ? $param['gouxing'] : '';

        $data['data'] = PengModel::alias('peng')
            ->field('peng.*,bill.sericnum')
            ->join('flow_bill bill','bill.id=peng.billid')
            ->where($where)
            ->order('peng.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['gouxing_name'] = (new OptService())->get_base_showname('peng_gouxing', $item['gouxing']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_dealcom(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['deal.deleted_user','=', 0];
        $where[] = ['deal.comid','=',$this->userinfo['comid']];
        $where[] = ['deal.isturn','=',1];
        $where[] = ['deal.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['deal.name|deal.ratenum|deal.phone|deal.lianer|deal.liantel','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['deal.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['deal.type','=', $param['type']];
        }
        if(isset($param['nature']) && !empty($param['nature'])){
            $where[] = ['deal.nature','=', $param['nature']];
        }
        if(isset($param['legaler']) && !empty($param['legaler'])){
            $where[] = ['deal.legaler','like', '%'.$param['legaler'].'%'];
        }
        if(isset($param['zhuaddr']) && !empty($param['zhuaddr'])){
            $where[] = ['deal.zhuaddr','like', '%'.$param['zhuaddr'].'%'];
        }
        if(isset($param['taxtype']) && !empty($param['taxtype'])){
            $where[] = ['deal.taxtype','=', $param['taxtype']];
        }
        if(isset($param['ratenum']) && !empty($param['ratenum'])){
            $where[] = ['deal.ratenum','like', '%'.$param['ratenum'].'%'];
        }
        if(isset($param['unitaddr']) && !empty($param['unitaddr'])){
            $where[] = ['deal.unitaddr','like', '%'.$param['unitaddr'].'%'];
        }
        if(isset($param['phone']) && !empty($param['phone'])){
            $where[] = ['deal.phone','like', '%'.$param['phone'].'%'];
        }
        if(isset($param['genre']) && !empty($param['genre'])){
            $where[] = ['deal.genre','like', '%'.$param['genre'].'%'];
        }
        if(isset($param['danlevel']) && !empty($param['danlevel'])){
            $where[] = ['deal.danlevel','=', $param['danlevel']];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['deal.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['liantel']) && !empty($param['liantel'])){
            $where[] = ['deal.liantel','like', '%'.$param['liantel'].'%'];
        }

        $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';

        $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
        $data['is_taxtype'] = isset($param['taxtype']) ? $param['taxtype'] : '';

        $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
        $data['is_nature'] = isset($param['nature']) ? $param['nature'] : '';

        $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
        $data['is_danlevel'] = isset($param['danlevel']) ? $param['danlevel'] : '';


        if(isset($param['lianer_zhi']) && !empty($param['lianer_zhi'])){
            $where[] = ['deal.lianer','=', $param['lianer_zhi']];
        }


        $enginid = $request->param('enginid',0,'int');
        $data['data'] = DealcomModel::alias('deal')
            ->field('deal.*')
            ->where($where)
            ->order('deal.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($enginid){
                $item['type_old'] = $item['type'];
                $item['nature_old'] = $item['nature'];
                $item['taxtype_old'] = $item['taxtype'];
                $item['danlevel_old'] = $item['danlevel'];

                $item['type'] = (new OptService())->get_base_showname('cai_dealcom_type', $item['type']);
                $item['nature'] = (new OptService())->get_base_showname('cai_dealcom_nature', $item['nature']);
                $item['taxtype'] = (new OptService())->get_base_showname('cai_dealcom_taxtype', $item['taxtype']);
                $item['danlevel'] = (new OptService())->get_base_showname('cai_dealcom_danlevel', $item['danlevel']);
//                $item['files'] = (new OptService())->get_file('dealcom', $item['id']);


//                $item['list'] = DealcomListModel::where(['dealcomid'=>$item['id']])->order('isdea DESC')->find();
//                $item['zheng'] = DealcomZhengModel::where(['dealcomid'=>$item['id']])->order('isdea DESC')->find();
//
//
//                //已付款累计(元)
//                $item['lei_kuan'] = HouChujinListModel::alias('list')
//                    ->join('hou_chujin hou','hou.id=list.chujinid')
//                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
//                    ->where(['list.fuer'=>$item['name']])
//                    ->sum('list.money');
//                //已收票累计(元)
//                $item['lei_piao'] = PiaoChuListModel::alias('list')
//                    ->join('piao_chu hou','hou.id=list.piaochuid')
//                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
//                    ->where(['list.xiaoer'=>$item['name']])
//                    ->sum('list.money');
//                //已付税款累计(元)
//                $item['lei_shui'] = HouChushuiListModel::alias('list')
//                    ->join('hou_chushui hou','hou.id=list.chushuiid')
//                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
//                    ->where(['list.fuer'=>$item['name']])
//                    ->sum('list.money');
//                //已签储备合同累计
//                $item['lei_chumoney'] = ContractChuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
//                    ->where(['isturn'=>1,'status'=>1,'gonger'=>$item['name']])
//                    ->sum('money');
//
//
//
//
//                //累计支付销售方
//                $item['gongzhifee'] = HouGongzhiListModel::alias('list')
//                    ->join('hou_gongzhi hou','hou.id=list.gongzhiid')
//                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
//                    ->where(['hou.enginid'=>$enginid,'list.fuer'=>$item['name']])
//                    ->sum('list.money');
//                //销售方进项票累计
//                $item['piaoshoufee'] = PiaoShouListModel::alias('list')
//                    ->join('piao_shou shou','shou.id=list.shouid')
//                    ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0,'shou.isturn'=>1,'shou.status'=>1])
//                    ->where(['shou.enginid'=>$enginid,'list.xiaoer'=>$item['name']])
//                    ->sum('list.money');
//                //已签采购合同金额
//                $item['concaimoney'] = (new HouService())->get_contract_cai_final_money($enginid,$item['name']);
//                //可抵扣进项票金额
//                $item['kedifee'] = 0;
//                if($item['gongzhifee'] > 0 && $item['piaoshoufee'] > 0){
//                    $item['kedifee'] = $item['gongzhifee'] > $item['piaoshoufee'] ? $item['piaoshoufee'] : $item['gongzhifee'];
//                }
//                //已抵扣进项票价税合计
//                $item['yidimoney'] = HouGongshuiListModel::alias('list')
//                    ->join('hou_gongshui hou','hou.id=list.gongshuiid')
//                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
//                    ->where(['hou.enginid'=>$enginid,'list.xiaoer'=>$item['name']])
//                    ->sum('list.money');
//                //已抵扣退税
//                $item['yidiratefee'] = HouGongshuiListModel::alias('list')
//                    ->join('hou_gongshui hou','hou.id=list.gongshuiid')
//                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
//                    ->where(['hou.enginid'=>$enginid,'list.xiaoer'=>$item['name']])
//                    ->sum('list.ratefee');
//
//                //剩余可抵扣进项票金额
//                $item['shengdifee'] = round($item['kedifee'] - $item['yidimoney'], 2);
//                $item['shengdifee'] = $item['shengdifee'] > 0 ? $item['shengdifee'] : 0;
//
//
//
//
//                $item['hougongyumoney'] = (new HouService())->get_hou_gongyu_money($item['name']);
//                $item['hougongzhimoneyqugoer'] = (new HouService())->get_hou_gongzhi_money_qugoer($item['name']);
//                $item['hougongyukoufee'] = (new HouService())->get_hou_gongyu_koufee($item['name']);
//                $item['hougongshuimoneygoquan'] = (new HouService())->get_hou_gongshui_money_goquan($item['name']);
//                $item['qizankoufee'] = HouKouQifeiModel::alias('list')
//                    ->join('hou_gongyu hou','hou.id=list.zhuid')
//                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
//                    ->where(['list.type'=>2, 'hou.gofuer'=>$item['name'],'kouxing'=>'暂扣'])
//                    ->sum('list.koufee');
//                $item['jiedongkoufee'] = HouKouQifeiModel::alias('list')
//                    ->join('hou_gongyu hou','hou.id=list.zhuid')
//                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
//                    ->where(['list.type'=>2, 'hou.gofuer'=>$item['name'],'isdong'=>1])
//                    ->sum('list.koufee');
//
//                $item['yuliufee'] = round($item['qizankoufee'] - $item['jiedongkoufee'], 2);
//                $item['hougongshuimoneygoquan'] = (new HouService())->get_hou_gongshui_money_goquan($item['name']);
//                $item['hougongyukeyong'] = round($item['hougongyumoney']-$item['hougongyukoufee']-$item['hougongzhimoneyqugoer']+$item['jiedongkoufee']+$item['hougongshuimoneygoquan'],2);
//



                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_dealcom_children(Request $request){
        $param = $request->post();
        $where[] = ['deal.id','in',$param['ids']];

        $data = DealcomModel::alias('deal')
            ->field('deal.*')
            ->where($where)
            ->select()->toArray();

        $enginid = $request->param('enginid',0,'int');
        foreach ($data as &$item){
            $item['type_old'] = $item['type'];
            $item['nature_old'] = $item['nature'];
            $item['taxtype_old'] = $item['taxtype'];
            $item['danlevel_old'] = $item['danlevel'];

            $item['type'] = (new OptService())->get_base_showname('cai_dealcom_type', $item['type']);
            $item['nature'] = (new OptService())->get_base_showname('cai_dealcom_nature', $item['nature']);
            $item['taxtype'] = (new OptService())->get_base_showname('cai_dealcom_taxtype', $item['taxtype']);
            $item['danlevel'] = (new OptService())->get_base_showname('cai_dealcom_danlevel', $item['danlevel']);
            $item['files'] = (new OptService())->get_file('dealcom', $item['id']);


            $item['list'] = DealcomListModel::where(['dealcomid'=>$item['id']])->order('isdea DESC')->find();
            $item['zheng'] = DealcomZhengModel::where(['dealcomid'=>$item['id']])->order('isdea DESC')->find();


            //已付款累计(元)
            $item['lei_kuan'] = HouChujinListModel::alias('list')
                ->join('hou_chujin hou','hou.id=list.chujinid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['list.fuer'=>$item['name']])
                ->sum('list.money');
            //已收票累计(元)
            $item['lei_piao'] = PiaoChuListModel::alias('list')
                ->join('piao_chu hou','hou.id=list.piaochuid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['list.xiaoer'=>$item['name']])
                ->sum('list.money');
            //已付税款累计(元)
            $item['lei_shui'] = HouChushuiListModel::alias('list')
                ->join('hou_chushui hou','hou.id=list.chushuiid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['list.fuer'=>$item['name']])
                ->sum('list.money');
            //已签储备合同累计
            $item['lei_chumoney'] = ContractChuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'gonger'=>$item['name']])
                ->sum('money');




            //累计支付销售方
            $item['gongzhifee'] = HouGongzhiListModel::alias('list')
                ->join('hou_gongzhi hou','hou.id=list.gongzhiid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['hou.enginid'=>$enginid,'list.fuer'=>$item['name']])
                ->sum('list.money');
            //销售方进项票累计
            $item['piaoshoufee'] = PiaoShouListModel::alias('list')
                ->join('piao_shou shou','shou.id=list.shouid')
                ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0,'shou.isturn'=>1,'shou.status'=>1])
                ->where(['shou.enginid'=>$enginid,'list.xiaoer'=>$item['name']])
                ->sum('list.money');
            //已签采购合同金额
            $item['concaimoney'] = (new HouService())->get_contract_cai_final_money($enginid,$item['name']);
            //可抵扣进项票金额
            $item['kedifee'] = 0;
            if($item['gongzhifee'] > 0 && $item['piaoshoufee'] > 0){
                $item['kedifee'] = $item['gongzhifee'] > $item['piaoshoufee'] ? $item['piaoshoufee'] : $item['gongzhifee'];
            }
            //已抵扣进项票价税合计
            $item['yidimoney'] = HouGongshuiListModel::alias('list')
                ->join('hou_gongshui hou','hou.id=list.gongshuiid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['hou.enginid'=>$enginid,'list.xiaoer'=>$item['name']])
                ->sum('list.money');
            //已抵扣退税
            $item['yidiratefee'] = HouGongshuiListModel::alias('list')
                ->join('hou_gongshui hou','hou.id=list.gongshuiid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['hou.enginid'=>$enginid,'list.xiaoer'=>$item['name']])
                ->sum('list.ratefee');

            //剩余可抵扣进项票金额
            $item['shengdifee'] = round($item['kedifee'] - $item['yidimoney'], 2);
            $item['shengdifee'] = $item['shengdifee'] > 0 ? $item['shengdifee'] : 0;




            $item['hougongyumoney'] = (new HouService())->get_hou_gongyu_money($item['name']);
            $item['hougongzhimoneyqugoer'] = (new HouService())->get_hou_gongzhi_money_qugoer($item['name']);
            $item['hougongyukoufee'] = (new HouService())->get_hou_gongyu_koufee($item['name']);
            $item['hougongshuimoneygoquan'] = (new HouService())->get_hou_gongshui_money_goquan($item['name']);
            $item['qizankoufee'] = HouKouQifeiModel::alias('list')
                ->join('hou_gongyu hou','hou.id=list.zhuid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['list.type'=>2, 'hou.gofuer'=>$item['name'],'kouxing'=>'暂扣'])
                ->sum('list.koufee');
            $item['jiedongkoufee'] = HouKouQifeiModel::alias('list')
                ->join('hou_gongyu hou','hou.id=list.zhuid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['list.type'=>2, 'hou.gofuer'=>$item['name'],'isdong'=>1])
                ->sum('list.koufee');

            $item['yuliufee'] = round($item['qizankoufee'] - $item['jiedongkoufee'], 2);
            $item['hougongshuimoneygoquan'] = (new HouService())->get_hou_gongshui_money_goquan($item['name']);
            $item['hougongyukeyong'] = round($item['hougongyumoney']-$item['hougongyukoufee']-$item['hougongzhimoneyqugoer']+$item['jiedongkoufee']+$item['hougongshuimoneygoquan'],2);


            $item['ban_count'] = (new QianService())->get_gofuer_ban_count($item['name']);
        }

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_dealcom_lianer(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['deal.deleted_user','=', 0];
        $where[] = ['deal.comid','=',$this->userinfo['comid']];
        $where[] = ['deal.isturn','=',1];
        $where[] = ['deal.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['deal.lianer|deal.liantel','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['deal.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['liantel']) && !empty($param['liantel'])){
            $where[] = ['deal.liantel','like', '%'.$param['liantel'].'%'];
        }

        $data['data'] = DealcomModel::alias('deal')
            ->field('deal.lianer,deal.liantel')
            ->where($where)
            ->order('deal.id DESC')
            ->group('lianer,liantel')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_dealcom_bank(Request $request){
        $param = $request->post();
        get_page($param);

        $data['dealcomid'] = 0;
        $data['list'] = [];

        $name = $request->post('name', 'string', '');
        $dealinfo = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1,'name'=>$name])->find();
        if(!$dealinfo){
            return outputJson(2000,lang('code_2000'),$data);
        }

        $whereRR = [];
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $whereRR[] = ['banknum','like', '%'.$param['banknum'].'%'];
        }


        $data['dealcomid'] = $dealinfo['id'];
        $list = DealcomListModel::where(['dealcomid'=>$dealinfo['id']])->where($whereRR)->order('isdea DESC')->select()->toArray();
        foreach ($list as &$item){
            $item['isdea_name'] = $item['isdea'] > 0 ? '默认' : '';
        }
        $data['list'] = $list;

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_dealcom_zheng(Request $request){
        $param = $request->post();
        get_page($param);

        $data['dealcomid'] = 0;
        $data['list'] = [];

        $name = $request->post('name', 'string', '');
        $dealinfo = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1,'name'=>$name])->find();
        if(!$dealinfo){
            return outputJson(2000,lang('code_2000'),$data);
        }

        $data['dealcomid'] = $dealinfo['id'];
        $list = DealcomZhengModel::where(['dealcomid'=>$dealinfo['id']])->order('isdea DESC')->select()->toArray();
        foreach ($list as &$item){
            $item['isdea_name'] = $item['isdea'] > 0 ? '默认' : '';
        }
        $data['list'] = $list;

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_engin(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['benenginid']) && !empty($param['benenginid'])){
            $bengofuer = EnginModel::where(['id'=>$param['benenginid']])->value('gofuer');
            $where[] = ['e.gofuer','=', $bengofuer];
        }


        if(isset($param['bengofuer']) && !empty($param['bengofuer'])){
            $where[] = ['e.gofuer','=', $param['bengofuer']];
        }


        if(isset($param['lantake']) && !empty($param['lantake'])){
            $where[] = ['e.lantake','=', $param['lantake']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['jiaer']) && !empty($param['jiaer'])){
            $where[] = ['e.jiaer','like', '%'.$param['jiaer'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['e.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.name','like', '%'.$param['gui_unit'].'%'];
        }
        if(isset($param['lanshi']) && !empty($param['lanshi'])){
            $where[] = ['e.lanshi','=', $param['lanshi']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['kailei']) && !empty($param['kailei'])){
            $where[] = ['e.kailei','=', $param['kailei']];
        }
        if(isset($param['win_unit']) && !empty($param['win_unit'])){
            $where[] = ['win_unit.name','like', '%'.$param['win_unit'].'%'];
        }
        if(isset($param['windt']) && !empty($param['windt'])){
            $where[] = ['e.windt','between', explode(' - ', $param['windt'])];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['status']) && !empty($param['status'])){
            $where[] = ['e.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }


        $data['lantake'] = (new EnginService())->get_engin_lantake();
        $data['is_lantake'] = isset($param['lantake']) ? $param['lantake'] : '';

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['win_unit'] = $data['chanid'];
        $data['is_win_unit'] = isset($param['win_unit']) ? $param['win_unit'] : '';
        $data['gui_unit'] = $data['chanid'];
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
        $data['is_lanshi'] = isset($param['lanshi']) ? $param['lanshi'] : '';

        $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
        $data['is_kailei'] = isset($param['kailei']) ? $param['kailei'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_zhaotype');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*,win_unit.name as win_unit_name,gui_unit.name as gui_unit_name')
            ->leftJoin('dept win_unit','win_unit.id=e.win_unit')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['yearcur'] = date('Y');
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $item['lanshi']);
                $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_zhaotype', $item['zhaotype']);

                $item['lantake_name'] = (new EnginService())->get_engin_lantake_name($item['lantake']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item);
                $item['state_name_wen'] = (new EnginService())->get_engin_state_name_tag($item);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);

                $item['gofutel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['name'=>$item['gofuer']])->value('liantel');

//                $item['neiinfo'] = ContractNeiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
//                    ->where(['isturn'=>1,'status'=>1])->find();
//
//                $item['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
//                    ->where(['isturn'=>1,'status'=>1])->find();
//                if($item['shiinfo']){
//                    $item['shiinfo']['chenger_name'] = DeptModel::get_name_by_pk($item['shiinfo']['chenger']);
//                    $item['shiinfo']['faerinfo'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$item['shiinfo']['faer'],'isturn'=>1,'status'=>1])->find();
//                }
//
//                $item['shijiefee'] = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
//                    ->where(['isturn'=>1,'status'=>1])->sum('money');
//
//
//                $item['hougonghuimoney'] = (new HouService())->get_hou_gonghui_money($item['id']);
//                $item['hougongzhimoneyquben'] = (new HouService())->get_hou_gongzhi_money_quben($item['id']);
//                $item['hougonghuikoufee'] = (new HouService())->get_hou_gonghui_koufee($item['id']);
//                $item['hougonghuikeyong'] = round($item['hougonghuimoney'] - $item['hougongzhimoneyquben'] - $item['hougonghuikoufee'], 2);
//
//                $gofuerinfo = [];
//                $gofuerinfo['hougongyumoney'] = (new HouService())->get_hou_gongyu_money($item['gofuer']);
//                $gofuerinfo['hougongyukoufee'] = (new HouService())->get_hou_gongyu_koufee($item['gofuer']);
//                $gofuerinfo['hougongzhimoneyqugoer'] = (new HouService())->get_hou_gongzhi_money_qugoer($item['gofuer']);
//                $gofuerinfo['hougongshuimoneygoquan'] = (new HouService())->get_hou_gongshui_money_goquan($item['gofuer']);
//                $gofuerinfo['hougongyukeyong'] = round($gofuerinfo['hougongyumoney']+$gofuerinfo['hougongshuimoneygoquan']-$gofuerinfo['hougongyukoufee']-$gofuerinfo['hougongzhimoneyqugoer'],2);
//                $item['gofuerinfo'] = $gofuerinfo;
//
//
//                $gofuerxianginfo = EnginModel::field('id,name')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
//                    ->where(['isturn'=>1,'status'=>1,'gofuer'=>$item['gofuer']])->select()->toArray();
//                foreach ($gofuerxianginfo as &$govv){
//                    $govv['hougonghuimoney'] = (new HouService())->get_hou_gonghui_money($govv['id']);
//                    $govv['hougongzhimoneyquben'] = (new HouService())->get_hou_gongzhi_money_quben($govv['id']);
//                    $govv['hougonghuikoufee'] = (new HouService())->get_hou_gonghui_koufee($govv['id']);
//                    $govv['hougonghuikeyong'] = round($govv['hougonghuimoney'] - $govv['hougongzhimoneyquben'] - $govv['hougonghuikoufee'], 2);
//                }
//                $item['gofuerxianginfo'] = $gofuerxianginfo;
//
//
//                $item['shangpiaokai'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
//                    ->where(['isturn'=>1,'status'=>1])->order('id DESC')->find();
//                if($item['shangpiaokai']){
//                    $item['shangpiaokai']['kaier_info'] = (new UserService())->get_users_part_arr($item['shangpiaokai']['kaier'], 'dept');
//                }
//
//                $item['last_kou_list'] = (new HouService())->get_engin_last_kou_list($item['id']);
//
//
//                //累计开票金额
//                $item['leikaifee'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
//                    ->where(['isturn'=>1,'status'=>1])->sum('money');
//
//
//                $item['piaokaifee'] = PiaoKaiListModel::alias('list')
//                    ->join('piao_kai kai','kai.id=list.kaiid')
//                    ->where(['kai.comid'=>$this->userinfo['comid'],'kai.deleted_user'=>0,'kai.isturn'=>1,'kai.status'=>1])
//                    ->where(['kai.enginid'=>$item['id']])
//                    ->sum('list.money');
//                //当前工程的工程销项票金额÷【结算 > 施工合同金额 > 中标价】×100%
//                $item['leixiaobi'] = 0;
//                if($item['shijiefee'] > 0){
//                    $item['leixiaobi'] = round($item['piaokaifee'] / $item['shijiefee'] * 100, 2);
//                }else{
//                    if($item['shiinfo'] && $item['shiinfo']['money'] > 0){
//                        $item['leixiaobi'] = round($item['piaokaifee'] / $item['shiinfo']['money'] * 100, 2);
//                    }else{
//                        if($item['winfee'] > 0){
//                            $item['leixiaobi'] = round($item['piaokaifee'] / $item['winfee'] * 100, 2);
//                        }
//                    }
//                }
//
//                //累计工程回款÷【结算 > 施工合同金额 > 中标价】X100%
//                $item['leihuibi'] = 0;
//                if($item['shijiefee'] > 0){
//                    $item['leihuibi'] = round($item['hougonghuimoney'] / $item['shijiefee']*100, 2);
//                }else{
//                    if($item['shiinfo'] && $item['shiinfo']['money'] > 0){
//                        $item['leihuibi'] = round($item['hougonghuimoney'] / $item['shiinfo']['money']*100, 2);
//                    }else{
//                        if($item['winfee'] > 0){
//                            $item['leihuibi'] = round($item['hougonghuimoney'] / $item['winfee']*100, 2);
//                        }
//                    }
//                }
//
//                $item['jiedongkoufee'] = HouKouQifeiModel::alias('list')
//                    ->join('hou_gonghui hou','hou.id=list.zhuid')
//                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
//                    ->where(['list.type'=>1, 'list.enginid'=>$item['id'],'isdong'=>1])
//                    ->sum('list.koufee');
//
//                $item['jiedongkoufee'] += HouKouQifeiModel::alias('list')
//                    ->join('hou_gongyu hou','hou.id=list.zhuid')
//                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
//                    ->where(['list.type'=>2, 'list.enginid'=>$item['id'],'isdong'=>1])
//                    ->sum('list.koufee');
//
//                $item['gonghui_keyu'] = round($item['hougonghuimoney'] - $item['hougongzhimoneyquben'] - $item['hougonghuikoufee'] + $item['jiedongkoufee'], 2);
//
//
//                $cert_use = [];
//                $useArr = CertUseModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'isreal'=>1,'enginid'=>$item['id']])->find();
//                if($useArr){
//                    $certuselist = CertUseListModel::where(['useid'=>$useArr['id']])->select()->toArray();
//                    foreach ($certuselist as $vvv){
//                        $certInfo = CertModel::where(['id'=>$vvv['certid']])->find();
//                        $vvv['name'] = $certInfo['name'];
//                        $vvv['number'] = $certInfo['number'];
//                        $vvv['major_name'] = str_replace('[]', ',', $certInfo['major']);
//                        $vvv['person_name'] = CertPersonModel::where(['id'=>$certInfo['personid']])->value('name');
//                        $vvv['statueuse_name'] = (new CertService())->get_cert_statueuse_name($certInfo['statueuse']);
//                        $vvv['chanid_name'] = DeptModel::get_name_by_pk($certInfo['chanid']);
//                        $cert_use[] = $vvv;
//                    }
//                }
//                $item['cert_use'] = $cert_use;


                return $item;
            })->toArray();


        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_children(Request $request){
        $param = $request->post();

        $where[] = ['e.id','in',$param['ids']];

        $data = EnginModel::alias('e')
            ->field('e.*,win_unit.name as win_unit_name,gui_unit.name as gui_unit_name')
            ->leftJoin('dept win_unit','win_unit.id=e.win_unit')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where($where)
            ->select()->toArray();

        foreach ($data as &$item){
            $item['yearcur'] = date('Y');
            $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

            $item['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $item['lanshi']);
            $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);
            $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_zhaotype', $item['zhaotype']);

            $item['lantake_name'] = (new EnginService())->get_engin_lantake_name($item['lantake']);
            $item['state_name'] = (new EnginService())->get_engin_state_name($item);
            $item['state_name_wen'] = (new EnginService())->get_engin_state_name_tag($item);
            $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);

            $item['gofutel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where(['name'=>$item['gofuer']])->value('liantel');

            $item['neiinfo'] = ContractNeiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1])->find();

            $item['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1])->find();
            if($item['shiinfo']){
                $item['shiinfo']['chenger_name'] = DeptModel::get_name_by_pk($item['shiinfo']['chenger']);
                $item['shiinfo']['faerinfo'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$item['shiinfo']['faer'],'isturn'=>1,'status'=>1])->find();
            }

            $item['qianfee'] = EnginKuaModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1])->sum('money');


            $item['shijiefee'] = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1])->sum('money');


            $item['hougonghuimoney'] = (new HouService())->get_hou_gonghui_money($item['id']);
            $item['hougongzhimoneyquben'] = (new HouService())->get_hou_gongzhi_money_quben($item['id']);
            $item['hougonghuikoufee'] = (new HouService())->get_hou_gonghui_koufee($item['id']);
            $item['hougonghuikeyong'] = round($item['hougonghuimoney'] - $item['hougongzhimoneyquben'] - $item['hougonghuikoufee'], 2);

            $gofuerinfo = [];
            $gofuerinfo['hougongyumoney'] = (new HouService())->get_hou_gongyu_money($item['gofuer']);
            $gofuerinfo['hougongyukoufee'] = (new HouService())->get_hou_gongyu_koufee($item['gofuer']);
            $gofuerinfo['hougongzhimoneyqugoer'] = (new HouService())->get_hou_gongzhi_money_qugoer($item['gofuer']);
            $gofuerinfo['hougongshuimoneygoquan'] = (new HouService())->get_hou_gongshui_money_goquan($item['gofuer']);
            $gofuerinfo['hougongyukeyong'] = round($gofuerinfo['hougongyumoney']+$gofuerinfo['hougongshuimoneygoquan']-$gofuerinfo['hougongyukoufee']-$gofuerinfo['hougongzhimoneyqugoer'],2);
            $item['gofuerinfo'] = $gofuerinfo;


            $gofuerxianginfo = EnginModel::field('id,name')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'gofuer'=>$item['gofuer']])->select()->toArray();
            foreach ($gofuerxianginfo as &$govv){
                $govv['hougonghuimoney'] = (new HouService())->get_hou_gonghui_money($govv['id']);
                $govv['hougongzhimoneyquben'] = (new HouService())->get_hou_gongzhi_money_quben($govv['id']);
                $govv['hougonghuikoufee'] = (new HouService())->get_hou_gonghui_koufee($govv['id']);
                $govv['hougonghuikeyong'] = round($govv['hougonghuimoney'] - $govv['hougongzhimoneyquben'] - $govv['hougonghuikoufee'], 2);
            }
            $item['gofuerxianginfo'] = $gofuerxianginfo;


            $item['shangpiaokai'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1])->order('id DESC')->find();
            if($item['shangpiaokai']){
                $item['shangpiaokai']['kaier_info'] = (new UserService())->get_users_part_arr($item['shangpiaokai']['kaier'], 'dept');
            }

            $item['last_kou_list'] = (new HouService())->get_engin_last_kou_list($item['id']);


            //累计开票金额
            $item['leikaifee'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1])->sum('money');


            $item['piaokaifee'] = PiaoKaiListModel::alias('list')
                ->join('piao_kai kai','kai.id=list.kaiid')
                ->where(['kai.comid'=>$this->userinfo['comid'],'kai.deleted_user'=>0,'kai.isturn'=>1,'kai.status'=>1])
                ->where(['kai.enginid'=>$item['id']])
                ->sum('list.money');
            //当前工程的工程销项票金额÷【结算 > 施工合同金额 > 中标价】×100%
            $item['leixiaobi'] = 0;
            if($item['shijiefee'] > 0){
                $item['leixiaobi'] = round($item['piaokaifee'] / $item['shijiefee'] * 100, 2);
            }else{
                if($item['shiinfo'] && $item['shiinfo']['money'] > 0){
                    $item['leixiaobi'] = round($item['piaokaifee'] / $item['shiinfo']['money'] * 100, 2);
                }else{
                    if($item['winfee'] > 0){
                        $item['leixiaobi'] = round($item['piaokaifee'] / $item['winfee'] * 100, 2);
                    }
                }
            }

            //累计工程回款÷【结算 > 施工合同金额 > 中标价】X100%
            $item['leihuibi'] = 0;
            if($item['shijiefee'] > 0){
                $item['leihuibi'] = round($item['hougonghuimoney'] / $item['shijiefee']*100, 2);
            }else{
                if($item['shiinfo'] && $item['shiinfo']['money'] > 0){
                    $item['leihuibi'] = round($item['hougonghuimoney'] / $item['shiinfo']['money']*100, 2);
                }else{
                    if($item['winfee'] > 0){
                        $item['leihuibi'] = round($item['hougonghuimoney'] / $item['winfee']*100, 2);
                    }
                }
            }

            $item['jiedongkoufee'] = HouKouQifeiModel::alias('list')
                ->join('hou_gonghui hou','hou.id=list.zhuid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['list.type'=>1, 'list.enginid'=>$item['id'],'isdong'=>1])
                ->sum('list.koufee');

            $item['jiedongkoufee'] += HouKouQifeiModel::alias('list')
                ->join('hou_gongyu hou','hou.id=list.zhuid')
                ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                ->where(['list.type'=>2, 'list.enginid'=>$item['id'],'isdong'=>1])
                ->sum('list.koufee');

            $item['gonghui_keyu'] = round($item['hougonghuimoney'] - $item['hougongzhimoneyquben'] - $item['hougonghuikoufee'] + $item['jiedongkoufee'], 2);


            $cert_use = [];
            $useArr = CertUseModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'isreal'=>1,'enginid'=>$item['id']])->find();
            if($useArr){
                $certuselist = CertUseListModel::where(['useid'=>$useArr['id']])->select()->toArray();
                foreach ($certuselist as $vvv){
                    $certInfo = CertModel::where(['id'=>$vvv['certid']])->find();
                    $vvv['name'] = $certInfo['name'];
                    $vvv['number'] = $certInfo['number'];
                    $vvv['major_name'] = str_replace('[]', ',', $certInfo['major']);
                    $vvv['person_name'] = CertPersonModel::where(['id'=>$certInfo['personid']])->value('name');
                    $vvv['statueuse_name'] = (new CertService())->get_cert_statueuse_name($certInfo['statueuse']);
                    $vvv['chanid_name'] = DeptModel::get_name_by_pk($certInfo['chanid']);
                    $cert_use[] = $vvv;
                }
            }
            $item['cert_use'] = $cert_use;





            $item['caifee'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'enginid'=>$item['id']])->sum('money');
            $item['caijiefee'] = ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'enginid'=>$item['id']])->sum('money');
            $item['piaoshoufee'] = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'enginid'=>$item['id']])->sum('money');
            $item['piaoshouratefee'] = PiaoShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'enginid'=>$item['id']])->sum('ratefee');
            $item['piaokaifee'] = PiaoKaiListModel::alias('list')
                ->join('piao_kai kai','kai.id=list.kaiid')
                ->where(['kai.comid'=>$this->userinfo['comid'],'kai.deleted_user'=>0,'kai.isturn'=>1,'kai.status'=>1])
                ->where(['kai.enginid'=>$item['id']])
                ->sum('list.money');
            $item['piaokairatefee'] = PiaoKaiListModel::alias('list')
                ->join('piao_kai kai','kai.id=list.kaiid')
                ->where(['kai.comid'=>$this->userinfo['comid'],'kai.deleted_user'=>0,'kai.isturn'=>1,'kai.status'=>1])
                ->where(['kai.enginid'=>$item['id']])
                ->sum('list.ratefee');

            $item['yingkoushuifee'] = 0;
            if($item['neiinfo']){
                $item['yingkoushuifee'] = round($item['piaokaifee']*$item['neiinfo']['rateshui']/100, 2);
            }

            $item['yikoushuifee'] = HouKouShuifeiModel::alias('list')
                ->field('list.*,e.name as enginname,e.gofuer,gui_unit.name as gui_unit_name
            ,hui.billid as billid_hui,hui.status as status_hui,hui.koufee as koufee_hui,hui.enginid as enginid_hui,hui.dt as dt_hui
            ,yu.billid as billid_yu,yu.status as status_yu,yu.koufee as koufee_yu,yu.gofuer as gofuer_yu,yu.dt as dt_yu')
                ->leftJoin('hou_gonghui hui','hui.id=list.zhuid and list.type=1')
                ->leftJoin('hou_gongyu yu','yu.id=list.zhuid and list.type=2')
                ->join('engin_shui shui','shui.id=list.shuiid')
                ->join('engin e','e.id=shui.enginid')
                ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
                ->where("hui.deleted_user = 0 or yu.deleted_user = 0")
                ->where("hui.comid = ".$this->userinfo['comid']." or yu.comid = ".$this->userinfo['comid'])
                ->where('hui.isturn|yu.isturn','=', 1)
                ->where('hui.status|yu.status','=', 1)
                ->where('list.houkoulei','=', 2)
                ->sum('list.koufee');
            $item['daikoushuifee'] = round($item['yingkoushuifee'] - $item['yikoushuifee'], 2);
            $item['daikoushuifee'] = $item['daikoushuifee'] < 0 ? 0 : $item['daikoushuifee'];
            $item['leigongshuifee'] = HouGongshuiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'enginid'=>$item['id']])->sum('money');

        }

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_engin_gong_tai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.id','=',$param['enginid']];

        $list = [];
        $engininfo = EnginModel::alias('e')
            ->field('e.id,e.name')
            ->where($where)
            ->order('e.id DESC')
            ->select()->toArray();
        foreach ($engininfo as $item){
            $dealList = [];

            //累计支付销售方
            $hougongzhi = HouGongzhiListModel::alias('list')
                ->field('list.fuer as gonger,sum(list.money) as hougongzhifee')
                ->join('hou_gongzhi zhi','zhi.id=list.gongzhiid')
                ->join('engin e','e.id=zhi.enginid')
                ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0,'zhi.isturn'=>1,'zhi.status'=>1])
                ->where(['zhi.enginid'=>$item['id']])
                ->group('list.fuer')
                ->select()->toArray();
            foreach ($hougongzhi as $hougongzhi_VV){
                $isGong = DealcomModel::where(['name'=>$hougongzhi_VV['gonger']])->find();
                if(!$isGong || $isGong['deleted_user'] != 0 || $isGong['isturn'] != 1 || $isGong['status'] != 1 || $isGong['type'] != 3){
                    continue;
                }

                $dealList[$hougongzhi_VV['gonger']] = $hougongzhi_VV;
            }


            //销售方进项票累计
            $piaoshou = PiaoShouListModel::alias('list')
                ->field('list.xiaoer as gonger,sum(list.money) as piaoshoufee')
                ->join('piao_shou shou','shou.id=list.shouid')
                ->join('engin e','e.id=shou.enginid')
                ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0,'shou.isturn'=>1,'shou.status'=>1])
                ->where(['shou.enginid'=>$item['id']])
                ->group('list.xiaoer')
                ->select()->toArray();
            foreach ($piaoshou as $piaoshou_VV){
                $isGong = DealcomModel::where(['name'=>$piaoshou_VV['gonger']])->find();
                if(!$isGong || $isGong['deleted_user'] != 0 || $isGong['isturn'] != 1 || $isGong['status'] != 1 || $isGong['type'] != 3){
                    continue;
                }

                if(isset($dealList[$piaoshou_VV['gonger']])){
                    $dealList[$piaoshou_VV['gonger']]['piaoshoufee'] = $piaoshou_VV['piaoshoufee'];
                }else{
                    $dealList[$piaoshou_VV['gonger']] = $piaoshou_VV;
                }
            }


            //销售方可签采购合同金额
            foreach ($dealList as &$en_VV){
                if(isset($en_VV['hougongzhifee']) && isset($en_VV['piaoshoufee'])){
                    $en_VV['keqiancai'] = $en_VV['hougongzhifee'] > $en_VV['piaoshoufee'] ? $en_VV['piaoshoufee'] : $en_VV['hougongzhifee'];
                }
            }


            //销售方已签采购合同金额
            $contractcai = ContractCaiModel::alias('cai')
                ->field('cai.gonger')
                ->join('engin e','e.id=cai.enginid')
                ->where(['cai.comid'=>$this->userinfo['comid'],'cai.deleted_user'=>0,'cai.isturn'=>1,'cai.status'=>1])
                ->where(['cai.enginid'=>$item['id']])
                ->group('cai.gonger')
                ->select()->toArray();
            foreach ($contractcai as $contractcai_VV){
                $isGong = DealcomModel::where(['name'=>$contractcai_VV['gonger']])->find();
                if(!$isGong || $isGong['deleted_user'] != 0 || $isGong['isturn'] != 1 || $isGong['status'] != 1 || $isGong['type'] != 3){
                    continue;
                }


                $contractcai_VV['yiqiancai'] = 0;
                $caiinfo = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['enginid'=>$item['id'],'gonger'=>$contractcai_VV['gonger']])->select()->toArray();
                foreach ($caiinfo as $caivvvvv){
                    $existJieMoney = ContractCaiJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                        ->where(['enginid'=>$item['id'],'caiid'=>$caivvvvv['id']])->sum('money');
                    if($existJieMoney){
                        $contractcai_VV['yiqiancai'] += $existJieMoney;
                    }else{
                        $contractcai_VV['yiqiancai'] += $caivvvvv['money'];
                    }
                }

                if(isset($dealList[$contractcai_VV['gonger']])){
                    $dealList[$contractcai_VV['gonger']]['yiqiancai'] = $contractcai_VV['yiqiancai'];
                }else{
                    $dealList[$contractcai_VV['gonger']] = $contractcai_VV;
                }
            }


            //销售方待签采购合同金额
            foreach ($dealList as $en22_KK => $en22_VV){
                if(!isset($en22_VV['keqiancai'])){
                    $en22_VV['keqiancai'] = 0;
                }
                if(!isset($en22_VV['yiqiancai'])){
                    $en22_VV['yiqiancai'] = 0;
                }
                $en22_VV['daiqiancai'] = round($en22_VV['keqiancai'] - $en22_VV['yiqiancai'], 2);
                $en22_VV['daiqiancai'] = $en22_VV['daiqiancai'] < 0 ? 0 : $en22_VV['daiqiancai'];
                if($en22_VV['daiqiancai']){
                    $dealList[$en22_KK]['daiqiancai'] = $en22_VV['daiqiancai'];
                }
            }


            //可抵扣进项票金额
            foreach ($dealList as &$en33_VV){
                if(isset($en33_VV['hougongzhifee']) && $en33_VV['hougongzhifee'] > 0
                    && isset($en33_VV['piaoshoufee']) && $en33_VV['piaoshoufee'] > 0
                    && isset($en33_VV['yiqiancai']) && $en33_VV['yiqiancai'] > 0) {
                    $en33_VV['kedijin'] = $en33_VV['hougongzhifee'] > $en33_VV['piaoshoufee'] ? $en33_VV['piaoshoufee'] : $en33_VV['hougongzhifee'];
                }
            }


            //已抵扣进项票价税合计
            $hougongshui = HouGongshuiListModel::alias('list')
                ->field('list.xiaoer as gonger,sum(list.money) as yidijin')
                ->join('hou_gongshui shui','shui.id=list.gongshuiid')
                ->join('engin e','e.id=shui.enginid')
                ->where(['shui.comid'=>$this->userinfo['comid'],'shui.deleted_user'=>0,'shui.isturn'=>1,'shui.status'=>1])
                ->where(['shui.enginid'=>$item['id']])
                ->group('list.xiaoer')
                ->select()->toArray();
            foreach ($hougongshui as $hougongshui_VV){
                $isGong = DealcomModel::where(['name'=>$hougongshui_VV['gonger']])->find();
                if(!$isGong || $isGong['deleted_user'] != 0 || $isGong['isturn'] != 1 || $isGong['status'] != 1 || $isGong['type'] != 3){
                    continue;
                }

                if(isset($dealList[$hougongshui_VV['gonger']])){
                    $dealList[$hougongshui_VV['gonger']]['yidijin'] = $hougongshui_VV['yidijin'];
                }else{
                    $dealList[$hougongshui_VV['gonger']] = $hougongshui_VV;
                }
            }

            //已抵扣退税
            $hougongshui22 = HouGongshuiListModel::alias('list')
                ->field('list.xiaoer as gonger,sum(list.ratefee) as yidirate')
                ->join('hou_gongshui shui','shui.id=list.gongshuiid')
                ->join('engin e','e.id=shui.enginid')
                ->where(['shui.comid'=>$this->userinfo['comid'],'shui.deleted_user'=>0,'shui.isturn'=>1,'shui.status'=>1])
                ->where(['shui.enginid'=>$item['id']])
                ->group('list.xiaoer')
                ->select()->toArray();
            foreach ($hougongshui22 as $hougongshui22_VV){
                $isGong = DealcomModel::where(['name'=>$hougongshui22_VV['gonger']])->find();
                if(!$isGong || $isGong['deleted_user'] != 0 || $isGong['isturn'] != 1 || $isGong['status'] != 1 || $isGong['type'] != 3){
                    continue;
                }

                if(isset($dealList[$hougongshui22_VV['gonger']])){
                    $dealList[$hougongshui22_VV['gonger']]['yidirate'] = $hougongshui22_VV['yidirate'];
                }else{
                    $dealList[$hougongshui22_VV['gonger']] = $hougongshui22_VV;
                }
            }


            //剩余可抵扣进项票金额
            foreach ($dealList as $en4444_KK => $en4444_VV){
                if(!isset($en4444_VV['kedijin'])){
                    $en4444_VV['kedijin'] = 0;
                }
                if(!isset($en4444_VV['yidijin'])){
                    $en4444_VV['yidijin'] = 0;
                }
                $en4444_VV['shengdijin'] = round($en4444_VV['kedijin'] - $en4444_VV['yidijin'], 2);
                $en4444_VV['shengdijin'] = $en4444_VV['shengdijin'] < 0 ? 0 : $en4444_VV['shengdijin'];
                if($en4444_VV['shengdijin']){
                    $dealList[$en4444_KK]['shengdijin'] = $en4444_VV['shengdijin'];
                }
            }






            if($dealList){
                foreach ($dealList as $dealList_VV){
                    $dealList_VV['enginname'] = $item['name'];
                    $list[] = $dealList_VV;
                }
            }else{
                $list[] = ['enginname'=>$item['name']];
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function get_engin_lian_unit(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.lian_unit','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }


        $data['data'] = EnginModel::alias('e')
            ->field('e.lian_unit')
            ->where($where)
            ->where((new MenuService())->get_opt_where('engin', 'e'))
            ->order('e.id DESC')
            ->group('e.lian_unit')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                $item['lian_unit_tel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['lian_unit']])
                    ->where(['status'=>1])->value('liantel');
                $item['lei_yushou'] = IncomeYushouModel::where(['lian_unit'=>$item['lian_unit'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('money');
                $item['lei_yuzhiall'] = (new PaymentService())->get_lian_unit_yuzhiall($item['lian_unit']);
                $item['lei_yubalance'] = round($item['lei_yushou'] - $item['lei_yuzhiall'], 2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_page_achieve(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.lantake','=',2];
        $where[] = ['e.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['kailei']) && !empty($param['kailei'])){
            $where[] = ['e.kailei','=', $param['kailei']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['status']) && !empty($param['status'])){
            $where[] = ['e.status','=', $param['status']];
        }

        $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
        $data['is_kailei'] = isset($param['kailei']) ? $param['kailei'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = EnginModel::alias('e')
            ->field('e.*,win_unit.name as win_unit_name,gui_unit.name as gui_unit_name')
            ->leftJoin('dept win_unit','win_unit.id=e.win_unit')
            ->leftJoin('dept gui_unit','gui_unit.id=e.gui_unit')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['yearwin'] = '';
                if($item['windt'] && $item['windt'] != '0000-00-00'){
                    $item['yearwin'] = date('Y', strtotime($item['windt']));
                }

                $item['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $item['lanshi']);
                $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);

                $item['lantake_name'] = (new EnginService())->get_engin_lantake_name($item['lantake']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item);
                $item['state_name_wen'] = (new EnginService())->get_engin_state_name_tag($item);

                $item['start_dt'] = '';
                $item['end_dt'] = '';
                $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
                if($shiinfo){
                    $item['start_dt'] = $shiinfo['startdt'];
                    $item['end_dt'] = $shiinfo['enddt'];
                }


                $item['jingcertinfo'] = '';
                $jingcert = CertUseListModel::alias('list')
                    ->join('cert_use use','use.id=list.useid')
                    ->where(['use.comid'=>$this->userinfo['comid'],'use.deleted_user'=>0])
                    ->where(['use.isturn'=>1,'use.status'=>1])
                    ->where(['use.enginid'=>$item['id']])
                    ->where(['use.isreal'=>1,'list.place'=>'项目经理'])
                    ->value('certid');
                if($jingcert){
                    $item['jingcertinfo'] = CertModel::where(['id'=>$jingcert])->find();
                    $item['jingcertinfo']['person_name'] = CertPersonModel::where(['id'=>$item['jingcertinfo']['personid']])->value('name');
                }

                $item['shucertinfo'] = '';
                $shucert = CertUseListModel::alias('list')
                    ->join('cert_use use','use.id=list.useid')
                    ->where(['use.comid'=>$this->userinfo['comid'],'use.deleted_user'=>0])
                    ->where(['use.isturn'=>1,'use.status'=>1])
                    ->where(['use.enginid'=>$item['id']])
                    ->where(['use.isreal'=>1,'list.place'=>'技术负责人'])
                    ->value('certid');
                if($shucert){
                    $item['shucertinfo'] = CertModel::where(['id'=>$shucert])->find();
                    $item['shucertinfo']['person_name'] = CertPersonModel::where(['id'=>$item['shucertinfo']['personid']])->value('name');
                }

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_page_achieve22(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['addr']) && !empty($param['addr'])){
            $where[] = ['e.addr','like', '%'.$param['addr'].'%'];
        }
        if(isset($param['expectfee']) && !empty($param['expectfee'])){
            $where[] = ['e.expectfee','like', '%'.$param['expectfee'].'%'];
        }
        if(isset($param['duration']) && !empty($param['duration'])){
            $where[] = ['e.duration','like', '%'.$param['duration'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['e.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['e.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['actualdt']) && !empty($param['actualdt'])){
            $where[] = ['e.actualdt','between', explode(' - ', $param['actualdt'])];
        }
        if(isset($param['filishdt']) && !empty($param['filishdt'])){
            $where[] = ['e.filishdt','between', explode(' - ', $param['filishdt'])];
        }



        // 用证信息查询
        if(isset($param['use_type']) && !empty($param['use_type'])){
            $use_type = $param['use_type'];
        }else{
            $use_type = '';
        }

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_opt_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use ($use_type){
                $item['yearcur'] = date('Y');
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['isting'] = $item['state'] == 3 ? true : false;

                $item['zhaotype'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);
                $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);

                $item['jingcertinfo'] = '';
                $jingcert = CertUseListModel::alias('list')
                    ->join('cert_use use','use.id=list.useid')
                    ->where(['use.comid'=>$this->userinfo['comid'],'use.deleted_user'=>0])
                    ->where(['use.isturn'=>1,'use.status'=>1])
                    ->where(['use.enginid'=>$item['id']])
                    ->where(['use.isreal'=>1,'list.place'=>'项目经理'])
                    ->value('certid');
                if($jingcert){
                    $item['jingcertinfo'] = CertModel::where(['id'=>$jingcert])->find();
                    $item['jingcertinfo']['person_name'] = CertPersonModel::where(['id'=>$item['jingcertinfo']['personid']])->value('name');
                }

                $item['shucertinfo'] = '';
                $shucert = CertUseListModel::alias('list')
                    ->join('cert_use use','use.id=list.useid')
                    ->where(['use.comid'=>$this->userinfo['comid'],'use.deleted_user'=>0])
                    ->where(['use.isturn'=>1,'use.status'=>1])
                    ->where(['use.enginid'=>$item['id']])
                    ->where(['use.isreal'=>1,'list.place'=>'项目技术负责人'])
                    ->value('certid');
                if($shucert){
                    $item['shucertinfo'] = CertModel::where(['id'=>$shucert])->find();
                    $item['shucertinfo']['person_name'] = CertPersonModel::where(['id'=>$item['shucertinfo']['personid']])->value('name');
                }


                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_page_piaokai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['addr']) && !empty($param['addr'])){
            $where[] = ['e.addr','like', '%'.$param['addr'].'%'];
        }
        if(isset($param['expectfee']) && !empty($param['expectfee'])){
            $where[] = ['e.expectfee','like', '%'.$param['expectfee'].'%'];
        }
        if(isset($param['duration']) && !empty($param['duration'])){
            $where[] = ['e.duration','like', '%'.$param['duration'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['e.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['e.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['actualdt']) && !empty($param['actualdt'])){
            $where[] = ['e.actualdt','between', explode(' - ', $param['actualdt'])];
        }
        if(isset($param['filishdt']) && !empty($param['filishdt'])){
            $where[] = ['e.filishdt','between', explode(' - ', $param['filishdt'])];
        }

        // 用证信息查询
        if(isset($param['use_type']) && !empty($param['use_type'])){
            $use_type = $param['use_type'];
        }else{
            $use_type = '';
        }

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_opt_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use ($use_type){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['isting'] = $item['state'] == 3 ? true : false;

                $item['engintype'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);

                //施工合同名称
                $item['shiname'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1])
                    ->where('status','in',[0,1])->value('name');
                $item['duier'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1])
                    ->where('status','in',[0,1])->value('faer');

                if($item['duier']){
                    $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['duier']])
                        ->where('status','in',[0,1])->find();
                    if($deal){
                        $item['ratenum'] = $deal['ratenum'];
                        $item['bankname'] = $deal['bankname'];
                        $item['banknum'] = $deal['banknum'];
                        $item['unitaddr'] = $deal['unitaddr'];
                        $item['phone'] = $deal['phone'];
                    }
                }

                $item['shichenger'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1])
                    ->where('status','in',[0,1])->value('chenger');
                $item['shichenger_name'] = DeptModel::get_name_by_pk($item['shichenger']);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_page_neikou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.business','=',2];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['addr']) && !empty($param['addr'])){
            $where[] = ['e.addr','like', '%'.$param['addr'].'%'];
        }
        if(isset($param['expectfee']) && !empty($param['expectfee'])){
            $where[] = ['e.expectfee','like', '%'.$param['expectfee'].'%'];
        }
        if(isset($param['duration']) && !empty($param['duration'])){
            $where[] = ['e.duration','like', '%'.$param['duration'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['e.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['e.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['actualdt']) && !empty($param['actualdt'])){
            $where[] = ['e.actualdt','between', explode(' - ', $param['actualdt'])];
        }
        if(isset($param['filishdt']) && !empty($param['filishdt'])){
            $where[] = ['e.filishdt','between', explode(' - ', $param['filishdt'])];
        }



        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_opt_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['isting'] = $item['state'] == 3 ? true : false;

                $item['engintype'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);

                $item['neilist'] = [];
                $neiid = ContractShiModel::where(['deleted_user'=>0,'enginid'=>$item['id'],'isturn'=>1,'status'=>1])->value('neiid');
                if($neiid){
                    $neilist = ContractNeiListModel::where(['neiid'=>$neiid])->select()->toArray();
                    foreach ($neilist as &$vvv){
                        $vvv['feiname_name'] = (new OptService())->get_base_showname('engin_nei_feiname', $vvv['feiname']);
                        $vvv['yingkou'] = (new ContractService())->get_contract_nei_feivalue($vvv, $item['id']);
                        $vvv['leikou'] = PaymentNeikouListModel::alias('list')
                            ->join('payment_neikou kou','kou.id=list.kouid')
                            ->where(['kou.comid'=>$this->userinfo['comid'],'kou.deleted_user'=>0,'list.listid'=>$vvv['id']])
                            ->where(['kou.isturn'=>1,'kou.status'=>1])
                            ->sum('list.money');

                        $vvv['daikou'] = round($vvv['yingkou']-$vvv['leikou'], 2);
                    }
                    $item['neilist'] = $neilist;
                }

                $item['shimoney'] = ContractShiModel::where(['enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->value('money');
                $item['piaokaifee'] = PiaokaiModel::where(['enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('money');
                $item['incomeenginfee'] = IncomeEnginModel::where(['enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('money');
                $item['lei_shikoufee'] = PaymentNeikouModel::where(['enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('shikoufee');
                $item['lei_zhankoufee'] = PaymentNeikouModel::where(['enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('zhankoufee');
                $item['lei_koufee'] = PaymentNeikouModel::where(['enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('money');


                $item['neifufee'] = PaymentNeifuModel::where(['enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('shenfee');
                $item['neikoufee'] = PaymentNeikouModel::where(['enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'qutype'=>1])->sum('money');
                $item['paymentenginfee'] = PaymentEnginListModel::alias('list')
                    ->join('payment_engin en','en.id=list.payenid')
                    ->where(['en.enginid'=>$item['id'],'en.deleted_user'=>0,'en.isturn'=>1,'en.status'=>1,'list.qutype'=>1])->sum('list.money');

                $item['gongyufee'] = round($item['incomeenginfee'] - $item['neifufee'] - $item['neikoufee'] - $item['paymentenginfee'], 2);


                $item['lei_yushou'] = IncomeYushouModel::where(['lian_unit'=>$item['lian_unit'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('money');
                $item['lei_yuzhiall'] = (new PaymentService())->get_lian_unit_yuzhiall($item['lian_unit']);
                $item['yushouyufee'] = round($item['lei_yushou'] - $item['lei_yuzhiall'], 2);


                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_page_jun(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];
        $where[] = ['e.state','=',6];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['addr']) && !empty($param['addr'])){
            $where[] = ['e.addr','like', '%'.$param['addr'].'%'];
        }
        if(isset($param['expectfee']) && !empty($param['expectfee'])){
            $where[] = ['e.expectfee','like', '%'.$param['expectfee'].'%'];
        }
        if(isset($param['duration']) && !empty($param['duration'])){
            $where[] = ['e.duration','like', '%'.$param['duration'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['e.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['e.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['actualdt']) && !empty($param['actualdt'])){
            $where[] = ['e.actualdt','between', explode(' - ', $param['actualdt'])];
        }
        if(isset($param['filishdt']) && !empty($param['filishdt'])){
            $where[] = ['e.filishdt','between', explode(' - ', $param['filishdt'])];
        }


        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';


        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $gckshou = (new BudgetService())->get_budshou_sons($gckid);

        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_opt_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($gckshou){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['isting'] = $item['state'] == 3 ? true : false;

                $item['engintype'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);

                $shiinfo = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
                if($shiinfo){
                    $shiinfo['chenger'] = DeptModel::get_name_by_pk($shiinfo['chenger']);
                }
                $item['shiinfo'] = $shiinfo;

                $item['shi_money'] = (new EnginService())->get_contract_shi_final_money($item['id']);
                $item['qian_money'] = VisaModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('shemoney');
                $item['chan_money'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->sum('leijin');
                $item['hui_money'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                    ->where('budid','in',$gckshou)->sum('money');


                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_page_xiang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['addr']) && !empty($param['addr'])){
            $where[] = ['e.addr','like', '%'.$param['addr'].'%'];
        }
        if(isset($param['expectfee']) && !empty($param['expectfee'])){
            $where[] = ['e.expectfee','like', '%'.$param['expectfee'].'%'];
        }
        if(isset($param['duration']) && !empty($param['duration'])){
            $where[] = ['e.duration','like', '%'.$param['duration'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['e.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['e.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['actualdt']) && !empty($param['actualdt'])){
            $where[] = ['e.actualdt','between', explode(' - ', $param['actualdt'])];
        }
        if(isset($param['filishdt']) && !empty($param['filishdt'])){
            $where[] = ['e.filishdt','between', explode(' - ', $param['filishdt'])];
        }


        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';




        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_opt_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['isting'] = $item['state'] == 3 ? true : false;

                $item['engintype'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);


                $xiang = LabourXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$item['id']])->find();
                $item['isxiang'] = $xiang ? true : false;
                if($item['isxiang']){
                    $item['gonger_info'] = LabourGongModel::field('id,name')
                        ->where('id','in',$xiang['gonger'])->select()->toArray();
                }

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_piao_kai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','=',1];
        $where[] = ['kai.enginid','=',$param['enginid']];




        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|kai.number|kai.duier|kai.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['kai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['kai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['piaotype']) && !empty($param['piaotype'])){
            $where[] = ['kai.piaotype','=', $param['piaotype']];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kai.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['ratenum']) && !empty($param['ratenum'])){
            $where[] = ['kai.ratenum','like', '%'.$param['ratenum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['kai.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['kai.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['unitaddr']) && !empty($param['unitaddr'])){
            $where[] = ['kai.unitaddr','like', '%'.$param['unitaddr'].'%'];
        }
        if(isset($param['phone']) && !empty($param['phone'])){
            $where[] = ['kai.phone','like', '%'.$param['phone'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kai.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['kai.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';




        $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
        $data['is_piaotype'] = isset($param['piaotype']) ? $param['piaotype'] : '';

        $whereDts = "";
//        $whereDts = "kai.dt is not null and kai.dt != '' and kai.dt != '0000-00-00'";

        $data['data'] = PiaokaiModel::alias('kai')
            ->field('kai.*,e.name as enginname')
            ->join('engin e','e.id=kai.enginid')
            ->where($where)
            ->where($whereDts)
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['piaotype'] = (new PiaoService())->get_kai_piaotype_name($item['piaotype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_piao_kai_list_page_gonghui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','=',1];
        $where[] = ['kai.enginid','=',$param['enginid']];
        $where[] = ['kai.ishong','=','否'];

        $list = [];
        $info = PiaoKaiListModel::alias('list')
            ->field('list.*,e.name as enginname,kai.shouer,kai.qianpiaotype,kai.dt')
            ->join('piao_kai kai','kai.id=list.kaiid')
            ->join('engin e','e.id=kai.enginid')
            ->where($where)->select()->toArray();

        foreach ($info as $item){
            $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);
            $item['sericnum'] = FlowBillModel::where(['modenum'=>'piao_kai','mid'=>$item['kaiid']])->value('sericnum');

            $item['yihuikuan'] = HouGonghuiListModel::alias('list')
                ->join('hou_gonghui hui','hui.id=list.gonghuiid')
                ->where(['hui.comid'=>$this->userinfo['comid'],'hui.deleted_user'=>0,'hui.isturn'=>1,'hui.status'=>1])
                ->where(['list.piaoid'=>$item['id']])
                ->sum('list.xiaofee');

            if($item['yihuikuan'] <= 0){
                $item['statename'] = '未回款';
            }else{
                if($item['yihuikuan'] < $item['money']){
                    $item['statename'] = '部分回款';
                }else{
                    continue;
                }
            }
            $item['daihuikuan'] = round($item['money'] - $item['yihuikuan'], 2);
            $list[] = $item;
        }


        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_piao_put_list(Request $request){
        $param = $request->post();

        $where[] = ['put.deleted_user','=', 0];
        $where[] = ['put.comid','=',$this->userinfo['comid']];
        $where[] = ['put.isturn','=',1];
        $where[] = ['put.status','=',1];
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['put.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|put.number|put.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['put.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['put.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $dtArr = explode(' - ', $param['created_at']);
            $where[] = ['put.created_at','between', [$dtArr[0].' 00:00:00', $dtArr[1].' 23:59:59']];
        }
        if(isset($param['ispay']) && $param['ispay'] != ''){
            $where[] = ['put.ispay','=', $param['ispay']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['put.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $list = [];
        $info = PiaoPutListModel::alias('list')
            ->field('put.*,e.name as enginname,list.gonger,list.dt,list.piaotype,list.id as listid,list.nums,list.fee
            ,list.rate,list.ratefee,list.money,list.dengdt,list.files,list.explain')
            ->join('piao_put put','put.id=list.putid')
            ->join('engin e','e.id=put.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_put', 'put'))
            ->order('put.id DESC')
            ->select()->toArray();

        foreach ($info as &$item){
            $kou_ratefee = PiaoKouListModel::alias('list')
                ->join('piao_kou zhu','zhu.id=list.kouid')
                ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1])
                ->where('zhu.status','in',[0,1])
                ->where(['list.listid'=>$item['listid']])
                ->sum('ratefee');
            if($kou_ratefee < $item['ratefee']){
                $item['kou_ratefee'] = $kou_ratefee;
                $item['ke_ratefee'] = round($item['ratefee'] - $item['kou_ratefee'], 2);

                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['filearr']);
                $item['piaotype_name'] = (new PiaoService())->get_put_piaotype_name($item['piaotype']);
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_archive(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['arc.deleted_user','=', 0];
        $where[] = ['arc.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['arc.name|arc.number|arc.idcard|arc.nation|arc.jiguan|arc.mian|arc.addr|arc.tel|arc.jiner|arc.jintel|arc.school|arc.major|arc.outexp|arc.socialareas|arc.shenum|arc.gongnum|arc.bankname|arc.banknum|arc.bankkai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['arc.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['arc.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['idcard']) && !empty($param['idcard'])){
            $where[] = ['arc.idcard','like', '%'.$param['idcard'].'%'];
        }
        if(isset($param['sex']) && !empty($param['sex'])){
            $where[] = ['arc.sex','=', $param['sex']];
        }
        if(isset($param['birdt']) && !empty($param['birdt'])){
            $where[] = ['arc.birdt','between', explode(' - ', $param['birdt'])];
        }
        if(isset($param['nation']) && !empty($param['nation'])){
            $where[] = ['arc.nation','like', '%'.$param['nation'].'%'];
        }
        if(isset($param['jiguan']) && !empty($param['jiguan'])){
            $where[] = ['arc.jiguan','like', '%'.$param['jiguan'].'%'];
        }
        if(isset($param['mian']) && !empty($param['mian'])){
            $where[] = ['arc.mian','like', '%'.$param['mian'].'%'];
        }
        if(isset($param['addr']) && !empty($param['addr'])){
            $where[] = ['arc.addr','like', '%'.$param['addr'].'%'];
        }
        if(isset($param['tel']) && !empty($param['tel'])){
            $where[] = ['arc.tel','like', '%'.$param['tel'].'%'];
        }
        if(isset($param['jiner']) && !empty($param['jiner'])){
            $where[] = ['arc.jiner','like', '%'.$param['jiner'].'%'];
        }
        if(isset($param['jintel']) && !empty($param['jintel'])){
            $where[] = ['arc.jintel','like', '%'.$param['jintel'].'%'];
        }
        if(isset($param['school']) && !empty($param['school'])){
            $where[] = ['arc.school','like', '%'.$param['school'].'%'];
        }
        if(isset($param['xueli']) && !empty($param['xueli'])){
            $where[] = ['arc.xueli','=', $param['xueli']];
        }
        if(isset($param['major']) && !empty($param['major'])){
            $where[] = ['arc.major','like', '%'.$param['major'].'%'];
        }
        if(isset($param['schooldt']) && !empty($param['schooldt'])){
            $where[] = ['arc.schooldt','between', explode(' - ', $param['schooldt'])];
        }
        if(isset($param['zhilevel']) && !empty($param['zhilevel'])){
            $where[] = ['arc.zhilevel','like', '%'.$param['zhilevel'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['arc.state','=', $param['state']];
        }
        if(isset($param['nature']) && !empty($param['nature'])){
            $where[] = ['arc.nature','=', $param['nature']];
        }
        if(isset($param['intodt']) && !empty($param['intodt'])){
            $where[] = ['arc.intodt','between', explode(' - ', $param['intodt'])];
        }
        if(isset($param['zhuandt']) && !empty($param['zhuandt'])){
            $where[] = ['arc.zhuandt','between', explode(' - ', $param['zhuandt'])];
        }
        if(isset($param['outdt']) && !empty($param['outdt'])){
            $where[] = ['arc.outdt','between', explode(' - ', $param['outdt'])];
        }
        if(isset($param['outexp']) && !empty($param['outexp'])){
            $where[] = ['arc.outexp','like', '%'.$param['outexp'].'%'];
        }
        if(isset($param['deptid']) && !empty($param['deptid'])){
            $where[] = ['arc.deptid','=', $param['deptid']];
        }
        $whereGangid = '';
        if(isset($param['gangid']) && $param['gangid'] != ''){
            $whereGangid = Db::raw("FIND_IN_SET(".$param['gangid'].",`arc`.`gangid`)");
        }
        if(isset($param['social']) && !empty($param['social'])){
            $where[] = ['arc.social','=', $param['social']];
        }
        if(isset($param['socialareas']) && !empty($param['socialareas'])){
            $where[] = ['arc.socialareas','like', '%'.$param['socialareas'].'%'];
        }
        if(isset($param['socstartdt']) && !empty($param['socstartdt'])){
            $where[] = ['arc.socstartdt','between', explode(' - ', $param['socstartdt'])];
        }
        if(isset($param['socenddt']) && !empty($param['socenddt'])){
            $where[] = ['arc.socenddt','between', explode(' - ', $param['socenddt'])];
        }
        if(isset($param['shenum']) && !empty($param['shenum'])){
            $where[] = ['arc.shenum','like', '%'.$param['shenum'].'%'];
        }
        if(isset($param['gongnum']) && !empty($param['gongnum'])){
            $where[] = ['arc.gongnum','like', '%'.$param['gongnum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['arc.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['arc.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankkai']) && !empty($param['bankkai'])){
            $where[] = ['arc.bankkai','like', '%'.$param['bankkai'].'%'];
        }

        $data['nature'] = (new OptService())->get_base_info('person_nature');
        $data['is_nature'] = isset($param['nature']) ? $param['nature'] : '';

        $data['social'] = (new OptService())->get_base_info('person_social');
        $data['is_social'] = isset($param['social']) ? $param['social'] : '';

        $data['xueli'] = (new OptService())->get_base_info('person_xueli');
        $data['is_xueli'] = isset($param['xueli']) ? $param['xueli'] : '';

        $data['gangArr'] = (new UserService())->get_usergang_all();
        $data['is_gangid'] = isset($param['gangid']) ? $param['gangid'] : '';




        $data['data'] = ArchiveModel::alias('arc')
            ->field('arc.*')
            ->where($where)
            ->where((new MenuService())->get_opt_where('archive', 'arc'))
            ->where($whereGangid)
            ->order('arc.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['deptid_name'] = DeptModel::get_name_by_pk($item['deptid']);
                $item['gangid_name'] = UserGangModel::get_name_by_ids($item['gangid'],' / ');
                $item['nature'] = (new OptService())->get_base_showname('person_nature', $item['nature']);
                $item['social'] = (new OptService())->get_base_showname('person_social', $item['social']);
                $item['xueli'] = (new OptService())->get_base_showname('person_xueli', $item['xueli']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_payment_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.created_user','=',$this->userinfo['id']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','=',1];

        if(isset($param['ismy']) && !empty($param['ismy'])){
            $where[] = ['jie.gonger','=', $this->userinfo['realname']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jie.number|jie.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['incyong']) && !empty($param['incyong'])){
            $where[] = ['jie.incyong','like', '%'.$param['incyong'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jie.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['jie.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['jie.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['jie.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['jie.bankname','like', '%'.$param['bankname'].'%'];
        }


        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';



        $data['data'] = PaymentJieModel::alias('jie')
            ->field('jie.*')
            ->where($where)
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>13,'mid'=>$item['id']])->sum('money');

                $item['difee'] = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$item['id']])->sum('difee');
                $item['difee'] += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$item['id']])->sum('difee');

                $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$item['id']])->column('id');
                $item['huanfee'] = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');

                $item['daihuan'] = $item['accmoney'] - $item['difee'] - $item['huanfee'];


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_payment_engin(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['pay.deleted_user','=', 0];
        $where[] = ['pay.comid','=',$this->userinfo['comid']];
        $where[] = ['pay.isturn','=',1];
        $where[] = ['pay.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['pay.enginid','=', $param['enginid']];
        }
        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where[] = ['pay.caiid','=', $param['caiid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|pay.number|pay.zhai|pay.money|pay.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['pay.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['budid']) && !empty($param['budid'])){
            $where[] = ['pay.budid','=', $param['budid']];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['pay.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['pay.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['pay.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['pay.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['pay.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['pay.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['pay.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['pay.status','=', $param['status']];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['budid'] = (new PaymentService())->get_payment_budid();
        $data['is_budid'] = isset($param['budid']) ? $param['budid'] : '';

        $data['incqing'] = (new OptService())->get_base_info('cai_income_incqing');
        $data['is_incqing'] = isset($param['incqing']) ? $param['incqing'] : '';

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $data['data'] = PaymentEnginModel::alias('pay')
            ->field('pay.*,e.name as enginname,cai.name as cainame')
            ->join('engin e','e.id=pay.enginid')
            ->leftjoin('contract_cai cai','cai.id=pay.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('payment_engin', 'pay'))
            ->order('pay.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budid_name'] = (new PaymentService())->get_payment_budid_name($item['budid']);
                $item['incqing'] = (new OptService())->get_base_showname('cai_income_incqing', $item['incqing']);
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accmoney'] = AccountListModel::where(['type'=>3,'mid'=>$item['id']])->sum('money');
                $item['daimoney'] = $item['money'] - $item['accmoney'];

                $item['yiju_stname'] = '';
                if($item['yiju'] == 'concaistuff'){
                    $item['yiju_stname'] = '材料合同';
                }elseif($item['yiju'] == 'stuffcai'){
                    $item['yiju_stname'] = '材料采购单';
                }elseif($item['yiju'] == 'stuffru'){
                    $item['yiju_stname'] = '材料入库单';
                }elseif($item['yiju'] == 'stuffjie'){
                    $item['yiju_stname'] = '材料结算单';
                }elseif($item['yiju'] == 'concailabour'){
                    $item['yiju_stname'] = '劳务合同';
                }elseif($item['yiju'] == 'labourshen'){
                    $item['yiju_stname'] = '劳务进度款申报';
                }elseif($item['yiju'] == 'labourjie'){
                    $item['yiju_stname'] = '劳务完工结算';
                }elseif($item['yiju'] == 'labouryong'){
                    $item['yiju_stname'] = '劳务用工单';
                }elseif($item['yiju'] == 'labourling'){
                    $item['yiju_stname'] = '零星劳务单';
                }elseif($item['yiju'] == 'concaidevice'){
                    $item['yiju_stname'] = '租赁合同';
                }elseif($item['yiju'] == 'devicejie'){
                    $item['yiju_stname'] = '租赁结算';
                }elseif($item['yiju'] == 'devicelin'){
                    $item['yiju_stname'] = '机械费登记';
                }elseif($item['yiju'] == 'concaisubbao'){
                    $item['yiju_stname'] = '分包合同';
                }elseif($item['yiju'] == 'subbaoshen'){
                    $item['yiju_stname'] = '分包进度款申报';
                }elseif($item['yiju'] == 'subbaojie'){
                    $item['yiju_stname'] = '分包进度款申报';
                }elseif($item['yiju'] == 'concaiqi'){
                    $item['yiju_stname'] = '其他采购合同';
                }

                $item['qingdt'] = date("Y-m-d", strtotime($item['created_at']));
                $item['paydt'] = AccountListModel::where(['type'=>3,'mid'=>$item['id']])->order('dt DESC')->value('dt');
                $item['paystatus'] = (new PaymentService())->get_paydata_paystatus_name($item['money'], $item['payfee']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


    public function get_seal(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['seal.deleted_user','=', 0];
        $where[] = ['seal.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['seal.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sealtype']) && !empty($param['sealtype'])){
            $where[] = ['seal.sealtype','=', $param['sealtype']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['seal.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['seal.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baoname']) && !empty($param['baoname'])){
            $where[] = ['bao.realname','like', '%'.$param['baoname'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['seal.state','=', $param['state']];
        }


        $data['state'] = (new XingService())->get_seal_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['sealtype'] = (new OptService())->get_base_info('qi_seal_sealtype');
        $data['is_sealtype'] = isset($param['sealtype']) ? $param['sealtype'] : '';


        $data['data'] = SealModel::alias('seal')
            ->field('seal.*,bao.realname as baoname')
            ->leftJoin('user bao','bao.id=seal.baoid')
            ->where($where)
            ->order('seal.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['sealtype'] = (new OptService())->get_base_showname('qi_seal_sealtype', $item['sealtype']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['wainame'] = (new XingService())->get_seal_wainame($item['name']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_book(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['book.deleted_user','=', 0];
        $where[] = ['book.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['book.number|book.name|book.author|book.chushe|book.isbn|book.cunaddr|ruer.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['booktype']) && !empty($param['booktype'])){
            $where[] = ['book.booktype','=', $param['booktype']];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['book.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['book.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['author']) && !empty($param['author'])){
            $where[] = ['book.author','like', '%'.$param['author'].'%'];
        }
        if(isset($param['chushe']) && !empty($param['chushe'])){
            $where[] = ['book.chushe','like', '%'.$param['chushe'].'%'];
        }
        if(isset($param['isbn']) && !empty($param['isbn'])){
            $where[] = ['book.isbn','like', '%'.$param['isbn'].'%'];
        }
        if(isset($param['rudt']) && !empty($param['rudt'])){
            $where[] = ['book.rudt','between', explode(' - ', $param['rudt'])];
        }
        if(isset($param['cunaddr']) && !empty($param['cunaddr'])){
            $where[] = ['book.cunaddr','like', '%'.$param['cunaddr'].'%'];
        }
        if(isset($param['ruername']) && !empty($param['ruername'])){
            $where[] = ['ruer.realname','like', '%'.$param['ruername'].'%'];
        }

        $data['booktype'] = (new OptService())->get_base_info('qi_booktype');
        $data['is_booktype'] = isset($param['booktype']) ? $param['booktype'] : '';

        $list = [];
        $info = BookModel::alias('book')
            ->field('book.*,ruer.realname as ruername')
            ->leftJoin('user ruer','ruer.id=book.ruer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('book', 'book'))
            ->order('book.id DESC')
            ->select()->toArray();
        foreach ($info as &$item){
            $state = (new XingService())->get_book_state($item['id']);
            if($state > 0){
                continue;
            }
            $item['booktype_name'] = (new OptService())->get_base_showname('qi_booktype', $item['booktype']);
            $item['state_name'] = '在册';

            $list[] = $item;
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_port(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['port.deleted_user','=', 0];
        $where[] = ['port.comid','=',$this->userinfo['comid']];
        $where[] = ['port.isturn','=',1];
        $where[] = ['port.status','=',1];
        $where[] = ['port.created_user','=',$this->userinfo['id']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['port.title','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['port.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['huidt']) && !empty($param['huidt'])){
            $where[] = ['port.huidt','between', explode(' - ', $param['huidt'])];
        }
        if(isset($param['huilei']) && $param['huilei'] != ''){
            $where[] = ['port.huilei','=', $param['huilei']];
        }
        if(isset($param['anlei']) && $param['anlei'] != ''){
            $where[] = ['port.anlei','=', $param['anlei']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['port.status','=', $param['status']];
        }

        $data['huilei'] = (new OptService())->get_base_info('port_huilei');
        $data['is_huilei'] = isset($param['huilei']) ? $param['huilei'] : '';

        $data['anlei'] = (new OptService())->get_base_info('port_anlei');
        $data['is_anlei'] = isset($param['anlei']) ? $param['anlei'] : '';

        $data['data'] = PortModel::alias('port')
            ->field('port.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('port', 'port'))
            ->order('port.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['huilei_name'] = (new OptService())->get_base_showname('port_huilei', $item['huilei']);
                $item['anlei_name'] = (new OptService())->get_base_showname('port_anlei', $item['anlei']);
                $item['qianid_name'] = PortModel::where(['id'=>$item['qianid']])->value('title');

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_port_list_an(Request $request){
        $param = $request->post();
        $portid = $request->post('portid',0,'int');
        $where[] = ['portid','=', $portid];

        $data['data'] = PortAnModel::where($where)
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function get_cert_employ(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['employ.deleted_user','=','0'];
        $where[] = ['employ.comid','=',$this->userinfo['comid']];


        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['employ.chanid','=', $param['chanid']];
        }
        if(isset($param['person_nature']) && !empty($param['person_nature'])){
            $where[] = ['person.nature','=', $param['person_nature']];
        }
        if(isset($param['person_name']) && !empty($param['person_name'])){
            $where[] = ['person.name','like', '%'.$param['person_name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['employ.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['sign_dt']) && !empty($param['sign_dt'])){
            $where[] = ['employ.sign_dt','=', $param['sign_dt']];
        }
        if(isset($param['qualification']) && !empty($param['qualification'])){
            $where[] = ['employ.qualification','like','%'.$param['qualification'].'%'];
        }
        if(isset($param['channel']) && !empty($param['channel'])){
            $where[] = ['employ.channel','eq',$param['channel']];
        }
        if(isset($param['purpose']) && !empty($param['purpose'])){
            $where[] = ['employ.purpose','eq',$param['purpose']];
        }
        if(isset($param['iszhu']) && !empty($param['iszhu'])){
            $where[] = ['employ.iszhu','eq',$param['iszhu']];
        }
        if(isset($param['pay_type']) && !empty($param['pay_type'])){
            $where[] = ['employ.pay_type','eq',$param['pay_type']];
        }
        if(isset($param['start_dt']) && !empty($param['start_dt'])){
            $where[] = ['employ.start_dt','=', $param['start_dt']];
        }
        if(isset($param['end_dt']) && !empty($param['end_dt'])){
            $where[] = ['employ.end_dt','=', $param['end_dt']];
        }
        if(isset($param['account_num']) && !empty($param['account_num'])){
            $where[] = ['employ.account_num','like', '%'.$param['account_num'].'%'];
        }


        $iszhu = CertEmployModel::get_iszhu();
        $pay_type = CertEmployModel::get_pay_type();
        $data['data'] = CertEmployModel::alias('employ')
            ->leftJoin('cert_person person','person.id = employ.personid')
            ->field('employ.*,person.name as person_name')
            ->where($where)
            ->where((new MenuService())->get_miss_where('cert_employ', 'employ'))
            ->order('employ.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use ($iszhu,$pay_type){
                $item['chan_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['iszhu_name'] = isset($iszhu[$item['iszhu']]) ? $iszhu[$item['iszhu']] : '';
                $item['pay_type'] = isset($pay_type[$item['pay_type']]) ? $pay_type[$item['pay_type']] : '';
                $item['person_nature'] = (new OptService())->get_base_showname('person_nature',$item['person_nature']);
                $item['channel'] = (new OptService())->get_base_showname('cert_employ_channel',$item['channel']);
                $item['purpose'] = (new OptService())->get_base_showname('cert_employ_purpose',$item['purpose']);

                $qualification = array_filter(explode(',',$item['qualification']));
                $item['qualification'] = '';
                foreach ($qualification as $v){
                    $item['qualification'] .= empty($item['qualification']) ? (new OptService())->get_base_showname('cert_employ_qualification', $v)
                        : '，'.(new OptService())->get_base_showname('cert_employ_qualification', $v);
                }
                return $item;
            })
            ->toArray();

        $data['iszhu'] = $iszhu;
        $data['channel'] = (new OptService())->get_base_info('cert_employ_channel');
        $data['qualification'] = (new OptService())->get_base_info('cert_employ_qualification');
        $data['purpose'] = (new OptService())->get_base_info('cert_employ_purpose');
        $data['pay_type'] = $pay_type;
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['person_nature'] = (new OptService())->get_base_info('person_nature');
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function get_doss(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['doss.deleted_user','=', 0];
        $where[] = ['doss.comid','=',$this->userinfo['comid']];
        $where[] = ['doss.isfei','=',0];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['doss.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|doss.number|doss.name|doss.xianger|doss.danglei|guier.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['doss.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['doss.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xianger']) && !empty($param['xianger'])){
            $where[] = ['doss.xianger','like', '%'.$param['xianger'].'%'];
        }
        if(isset($param['danglei']) && !empty($param['danglei'])){
            $where[] = ['doss.danglei','like', '%'.$param['danglei'].'%'];
        }
        if(isset($param['guiername']) && !empty($param['guiername'])){
            $where[] = ['guier.realname','like', '%'.$param['guiername'].'%'];
        }
        if(isset($param['dangdt']) && !empty($param['dangdt'])){
            $where[] = ['doss.dangdt','between', explode(' - ', $param['dangdt'])];
        }
        if(isset($param['guidt']) && !empty($param['guidt'])){
            $where[] = ['doss.guidt','between', explode(' - ', $param['guidt'])];
        }
        if(isset($param['dangfen']) && !empty($param['dangfen'])){
            $where[] = ['doss.dangfen','=', $param['dangfen']];
        }

        $data['dangfen'] = (new OptService())->get_base_info('doss_dangfen');
        $data['is_dangfen'] = isset($param['dangfen']) ? $param['dangfen'] : '';



        $list = [];
        $info = DossModel::alias('doss')
            ->field('doss.*,guier.realname as guiername,e.name as enginname')
            ->join('user guier','guier.id=doss.guier')
            ->leftJoin('engin e','e.id=doss.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('doss', 'doss'))
            ->order('doss.id DESC')
            ->select()->toArray();
        foreach ($info as &$item){
            $item['dangfen_name'] = (new OptService())->get_base_showname('doss_dangfen', $item['dangfen']);

            $item['yijie'] = DossJieListModel::alias('list')
                ->join('doss_jie jie','jie.id=list.jieid')
                ->where(['jie.comid'=>$this->userinfo['comid'],'jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1])
                ->where(['list.dossid'=>$item['id'],'list.state'=>0])
                ->sum('list.num');

            $item['shenjie'] = DossJieListModel::alias('list')
                ->join('doss_jie jie','jie.id=list.jieid')
                ->where(['jie.comid'=>$this->userinfo['comid'],'jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                ->where(['list.dossid'=>$item['id']])
                ->sum('list.num');

            $item['zaiku'] = round($item['guinum'] - $item['yijie'], 2);
            $item['kejie'] = round($item['guinum'] - $item['yijie'] - $item['shenjie'], 2);

            if($item['kejie'] > 0){
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_coss(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['coss.deleted_user','=', 0];
        $where[] = ['coss.comid','=',$this->userinfo['comid']];
        $where[] = ['coss.isfei','=',0];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['coss.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|coss.number|coss.name|coss.xianger|coss.danglei|guier.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['coss.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['coss.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xianger']) && !empty($param['xianger'])){
            $where[] = ['coss.xianger','like', '%'.$param['xianger'].'%'];
        }
        if(isset($param['danglei']) && !empty($param['danglei'])){
            $where[] = ['coss.danglei','like', '%'.$param['danglei'].'%'];
        }
        if(isset($param['guiername']) && !empty($param['guiername'])){
            $where[] = ['guier.realname','like', '%'.$param['guiername'].'%'];
        }
        if(isset($param['dangdt']) && !empty($param['dangdt'])){
            $where[] = ['coss.dangdt','between', explode(' - ', $param['dangdt'])];
        }
        if(isset($param['guidt']) && !empty($param['guidt'])){
            $where[] = ['coss.guidt','between', explode(' - ', $param['guidt'])];
        }
        if(isset($param['dangfen']) && !empty($param['dangfen'])){
            $where[] = ['coss.dangfen','=', $param['dangfen']];
        }

        $data['dangfen'] = (new OptService())->get_base_info('coss_dangfen');
        $data['is_dangfen'] = isset($param['dangfen']) ? $param['dangfen'] : '';



        $list = [];
        $info = CossModel::alias('coss')
            ->field('coss.*,guier.realname as guiername,e.name as enginname')
            ->join('user guier','guier.id=coss.guier')
            ->leftJoin('engin e','e.id=coss.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('coss', 'coss'))
            ->order('coss.id DESC')
            ->select()->toArray();
        foreach ($info as &$item){
            $item['dangfen_name'] = (new OptService())->get_base_showname('xoss_dangfen', $item['dangfen']);

            $item['yijie'] = CossJieListModel::alias('list')
                ->join('coss_jie jie','jie.id=list.jieid')
                ->where(['jie.comid'=>$this->userinfo['comid'],'jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1])
                ->where(['list.cossid'=>$item['id'],'list.state'=>0])
                ->sum('list.num');

            $item['shenjie'] = CossJieListModel::alias('list')
                ->join('coss_jie jie','jie.id=list.jieid')
                ->where(['jie.comid'=>$this->userinfo['comid'],'jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                ->where(['list.cossid'=>$item['id']])
                ->sum('list.num');

            $item['zaiku'] = round($item['guinum'] - $item['yijie'], 2);
            $item['kejie'] = round($item['guinum'] - $item['yijie'] - $item['shenjie'], 2);

            if($item['kejie'] > 0){
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_xoss(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['xoss.deleted_user','=', 0];
        $where[] = ['xoss.comid','=',$this->userinfo['comid']];
        $where[] = ['xoss.isfei','=',0];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['xoss.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|xoss.number|xoss.name|xoss.xianger|xoss.danglei|guier.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['xoss.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['xoss.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xianger']) && !empty($param['xianger'])){
            $where[] = ['xoss.xianger','like', '%'.$param['xianger'].'%'];
        }
        if(isset($param['danglei']) && !empty($param['danglei'])){
            $where[] = ['xoss.danglei','like', '%'.$param['danglei'].'%'];
        }
        if(isset($param['guiername']) && !empty($param['guiername'])){
            $where[] = ['guier.realname','like', '%'.$param['guiername'].'%'];
        }
        if(isset($param['dangdt']) && !empty($param['dangdt'])){
            $where[] = ['xoss.dangdt','between', explode(' - ', $param['dangdt'])];
        }
        if(isset($param['guidt']) && !empty($param['guidt'])){
            $where[] = ['xoss.guidt','between', explode(' - ', $param['guidt'])];
        }
        if(isset($param['dangfen']) && !empty($param['dangfen'])){
            $where[] = ['xoss.dangfen','=', $param['dangfen']];
        }

        $data['dangfen'] = (new OptService())->get_base_info('xoss_dangfen');
        $data['is_dangfen'] = isset($param['dangfen']) ? $param['dangfen'] : '';



        $list = [];
        $info = XossModel::alias('xoss')
            ->field('xoss.*,guier.realname as guiername,e.name as enginname')
            ->join('user guier','guier.id=xoss.guier')
            ->leftJoin('engin e','e.id=xoss.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('xoss', 'xoss'))
            ->order('xoss.id DESC')
            ->select()->toArray();
        foreach ($info as &$item){
            $item['dangfen_name'] = (new OptService())->get_base_showname('xoss_dangfen', $item['dangfen']);

            $item['yijie'] = XossJieListModel::alias('list')
                ->join('xoss_jie jie','jie.id=list.jieid')
                ->where(['jie.comid'=>$this->userinfo['comid'],'jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1])
                ->where(['list.xossid'=>$item['id'],'list.state'=>0])
                ->sum('list.num');

            $item['shenjie'] = XossJieListModel::alias('list')
                ->join('xoss_jie jie','jie.id=list.jieid')
                ->where(['jie.comid'=>$this->userinfo['comid'],'jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                ->where(['list.xossid'=>$item['id']])
                ->sum('list.num');

            $item['zaiku'] = round($item['guinum'] - $item['yijie'], 2);
            $item['kejie'] = round($item['guinum'] - $item['yijie'] - $item['shenjie'], 2);

            if($item['kejie'] > 0){
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_dang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['dang.deleted_user','=', 0];
        $where[] = ['dang.comid','=',$this->userinfo['comid']];
        $where[] = ['dang.isjie','=','是'];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['dang.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['dangtype']) && !empty($param['dangtype'])){
            $where[] = ['dang.dangtype','=', $param['dangtype']];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['dang.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['num']) && !empty($param['num'])){
            $where[] = ['dang.num','like', '%'.$param['num'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['dang.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['dang.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['dangcun']) && !empty($param['dangcun'])){
            $where[] = ['dang.dangcun','=', $param['dangcun']];
        }



        $data['dangtype'] = (new OptService())->get_base_info('qi_dang_dangtype');
        $data['is_dangtype'] = isset($param['dangtype']) ? $param['dangtype'] : '';

        $data['dangcun'] = (new OptService())->get_base_info('qi_dang_dangcun');
        $data['is_dangcun'] = isset($param['dangcun']) ? $param['dangcun'] : '';


        $data['data'] = DangModel::alias('dang')
            ->field('dang.*')
            ->where($where)
            ->order('dang.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['dangtype'] = (new OptService())->get_base_showname('qi_dang_dangtype', $item['dangtype']);
                $item['dangcun'] = (new OptService())->get_base_showname('qi_dang_dangcun', $item['dangcun']);

                $item['jienum'] =  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1,'jie.isjie'=>1])
                    ->where("list.fandt != '' and list.fandt is not null")
                    ->where('list.dangid','=',$item['id'])
                    ->sum('list.num');
                $item['jienum'] +=  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                    ->where('list.dangid','=',$item['id'])
                    ->sum('list.num');

                $item['kejie'] = $item['num'] - $item['jienum'];

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_wu_cai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.flowuser|cai.flowdept|cai.number|cai.title|cai.gonger|cai.gongtel|cai.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['cai.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['flowdept']) && !empty($param['flowdept'])){
            $where[] = ['cai.flowdept','like', '%'.$param['flowdept'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['cai.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['gongtel']) && !empty($param['gongtel'])){
            $where[] = ['cai.gongtel','like', '%'.$param['gongtel'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['cai.money','like', '%'.$param['money'].'%'];
        }


        $data['data'] = WuCaiModel::alias('cai')
            ->field('cai.*')
            ->where($where)
            ->where((new MenuService())->get_opt_where('wu_cai', 'cai'))
            ->order('cai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['list'] = WuCaiListModel::where(['caiid'=>$item['id']])->select()->toArray();
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_wu_mingxi(Request $request){
        $param = $request->post();
        get_page($param);

        $typess = $request->post('typess',0,'int');

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=',1];

        if(isset($param['cangku']) && !empty($param['cangku'])){
            $where[] = ['ru.cangku','=', $param['cangku']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['list.name|list.unit|list.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['list.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['unit']) && !empty($param['unit'])){
            $where[] = ['list.unit','like', '%'.$param['unit'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['list.gonger','like', '%'.$param['gonger'].'%'];
        }


        $list = [];

        $info = WuRuListModel::alias('list')
            ->field('list.*,sum(num) as runum,ru.cangku')
            ->join('wu_ru ru','ru.id=list.ruid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('wu_guan', 'ru'))
            ->order('list.id DESC')
            ->group('ru.cangku,list.name,list.unit,list.rufen')
            ->select()->toArray();

        foreach ($info as $item){
            $item['cangku_name'] = (new OptService())->get_base_showname('wu_cangku', $item['cangku']);
            $item['rufen_name'] = (new OptService())->get_base_showname('wu_rufen', $item['rufen']);
            $item['chunum'] = WuLingListModel::alias('list')
                ->join('wu_ling ling','ling.id=list.lingid')
                ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>1])
                ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                ->where(['list.rufen'=>$item['rufen'],'list.cangku'=>$item['cangku']])
                ->sum('list.num');


            $item['kucun'] = round($item['runum']-$item['chunum'], 2);

            $item['lingshen'] = WuLingListModel::alias('list')
                ->join('wu_ling ling','ling.id=list.lingid')
                ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>0])
                ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                ->where(['list.rufen'=>$item['rufen'],'list.cangku'=>$item['cangku']])
                ->sum('list.num');

            $item['kechu'] = round($item['runum']-$item['chunum']-$item['lingshen'], 2);

            if($typess > 0){
                if($item['kechu'] > 0){
                    $list[] = $item;
                }
            }else{
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_engin_bond_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|shou.number|shou.zhai|shou.faer|shou.money|acc.accname|shou.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name|bid.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number|bid.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['shou.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['shou.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['shou.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['shou.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['shou.baner','like', '%'.$param['baner'].'%'];
        }


        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $whereXiang = "";
        if($this->userinfo['issuper'] < 1){
            $enginArr = (new EnginService())->get_user_engin();
            $enginArrStr = implode(',', $enginArr);
            if($enginArrStr){
                $enginStr = '('.$enginArrStr.')';
            }else{
                $enginStr = '(0)';
            }

            $bidArr = (new BidService())->get_user_bid();
            $bidArrStr = implode(',', $bidArr);
            if($bidArrStr){
                $bidStr = '('.$bidArrStr.')';
            }else{
                $bidStr = '(0)';
            }

            $whereXiang = '(e.id in '.$enginStr.' or bid.id in '.$bidStr.')';
        }


        $data['data'] = EnginBondShouModel::alias('shou')
            ->field('shou.*,e.name as enginname,acc.name as accname,acc.accnum,bid.name as bidname')
            ->leftJoin('engin e','e.id=shou.enginid')
            ->leftJoin('bid bid','bid.id=shou.bidid')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->where($where)
            ->where($whereXiang)
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }
                $item['bondtype'] = (new PaymentService())->get_engin_bondtype_name($item['bondtype']);
                $item['budid_name'] = (new BudgetService())->get_budid_quanname($item['budid']);
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }



    public function get_account(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['acc.deleted_user','=', 0];
        $where[] = ['acc.comid','=',$this->userinfo['comid']];
        $where[] = ['acc.status','=',1];

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['acc.name|acc.accname|acc.accnum','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['acc.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['accnum']) && !empty($param['accnum'])){
            $where[] = ['acc.accnum','like', '%'.$param['accnum'].'%'];
        }
        if(isset($param['acctype']) && !empty($param['acctype'])){
            $where[] = ['acc.acctype','=', $param['acctype']];
        }
        if(isset($param['accmoney']) && !empty($param['accmoney'])){
            $where[] = ['acc.accmoney','like', '%'.$param['accmoney'].'%'];
        }
        if(isset($param['accdt']) && !empty($param['accdt'])){
            $where[] = ['acc.accdt','between', explode(' - ', $param['accdt'])];
        }


        $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
        $data['is_acctype'] = isset($param['acctype']) ? $param['acctype'] : '';




        $data['data'] = AccountModel::alias('acc')
            ->field('acc.*,e.name as enginname')
            ->leftJoin('engin e','e.id=acc.enginid')
            ->where($where)
            ->where((new MenuService())->get_opt_where('account', 'acc'))
            ->order('acc.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['acctype'] = (new OptService())->get_base_showname('cai_account_acctype', $item['acctype']);
                $item['chanid_name'] = DeptModel::where(['id'=>$item['chanid']])->value('name');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_car(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['car.deleted_user','=', 0];
        $where[] = ['car.comid','=',$this->userinfo['comid']];
        $where[] = ['car.state','=','正常'];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['car.paihao|car.pinpai|car.suoren|guan.realname','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['paihao']) && !empty($param['paihao'])){
            $where[] = ['car.paihao','like', '%'.$param['paihao'].'%'];
        }
        if(isset($param['pinpai']) && !empty($param['pinpai'])){
            $where[] = ['car.pinpai','like', '%'.$param['pinpai'].'%'];
        }
        if(isset($param['suoren']) && !empty($param['suoren'])){
            $where[] = ['car.suoren','like', '%'.$param['suoren'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['car.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['jiandt']) && !empty($param['jiandt'])){
            $where[] = ['car.jiandt','between', explode(' - ', $param['jiandt'])];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['car.state','=', $param['state']];
        }
        if(isset($param['usedeptid']) && !empty($param['usedeptid'])){
            $where[] = ['car.usedeptid','=', $param['usedeptid']];
        }
        if(isset($param['guanid_name']) && !empty($param['guanid_name'])){
            $where[] = ['guan.realname','like', '%'.$param['guanid_name'].'%'];
        }


        $data['state'] = (new XingService())->get_car_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['usedeptid'] = (new UserService())->get_deptinfo_all();;
        $data['is_usedeptid'] = isset($param['usedeptid']) ? $param['usedeptid'] : '';

        $data['data'] = CarModel::alias('car')
            ->field('car.*,guan.realname as guanid_name')
            ->leftJoin('user guan','guan.id=car.guanid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('car', 'car'))
            ->order('car.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['usedeptid_name'] = DeptModel::get_name_by_pk($item['usedeptid']);
                $item['jiayufee'] = (new XingService())->get_car_jiayufee($item['id']);
                $item['xiashu'] = (new XingService())->get_car_xiashu($item['id']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_engin_xun(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['xun.deleted_user','=', 0];
        $where[] = ['xun.comid','=',$this->userinfo['comid']];
        $where[] = ['xun.isturn','=',1];
        $where[] = ['xun.status','=',1];
        $where[] = ['xun.enginid','=',$param['enginid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['xun.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['xun.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['xuntype']) && !empty($param['xuntype'])){
            $where[] = ['xun.xuntype','=', $param['xuntype']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['xun.dt','between', explode(' - ', $param['dt'])];
        }

        $wherexundept = '';
        if(isset($param['xundept']) && !empty($param['xundept'])){
            $wherexundept = Db::raw("find_in_set(".$param['xundept'].",xundept)");
        }

        $wherexuner = '';
        if(isset($param['xuner']) && !empty($param['xuner'])){
            $wherexuner = Db::raw("find_in_set(".$param['xuner'].",xuner)");
        }

        $data['xuntype'] = (new OptService())->get_base_info('scene_xuntype');
        $data['is_xuntype'] = isset($param['xuntype']) ? $param['xuntype'] : '';

        $data['data'] = EnginXunModel::alias('xun')
            ->field('xun.*,e.name as enginname')
            ->join('engin e','e.id=xun.enginid')
            ->where($where)
            ->where($wherexundept)
            ->where($wherexuner)
            ->order('xun.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['xuntype'] = (new OptService())->get_base_showname('scene_xuntype', $item['xuntype']);
                $item['xundept_name'] = DeptModel::get_name_by_ids($item['xundept']);
                $item['xuner_name'] = UserModel::get_realname_by_ids($item['xuner']);

                $list = EnginXunListModel::where(['xunid'=>$item['id']])->select()->toArray();
                foreach ($list as &$vv){
                    $vv['filearr'] = FileModel::where('id','in',$vv['files'])->select()->toArray();
                }
                $item['list'] = $list;


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_engin_xun_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['xunid','=', $param['xunid']];
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['name','like', '%'.$param['keyword'].'%'];
        }

        $data['data'] = EnginXunListModel::where($where)
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_engin_jian(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jian.deleted_user','=', 0];
        $where[] = ['jian.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jian.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['jian.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['jiantype']) && !empty($param['jiantype'])){
            $where[] = ['jian.jiantype','=', $param['jiantype']];
        }

        $data['jiantype'] = (new OptService())->get_base_info('scene_jiantype');
        $data['is_jiantype'] = isset($param['jiantype']) ? $param['jiantype'] : '';

        $data['data'] = EnginJianModel::alias('jian')
            ->field('jian.*')
            ->where($where)
            ->order('jian.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['jiantype'] = (new OptService())->get_base_showname('scene_jiantype', $item['jiantype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }





    public function get_income_engin_dai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['inc.deleted_user','=', 0];
        $where[] = ['inc.comid','=',$this->userinfo['comid']];
        $where[] = ['inc.mal','=',0];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['inc.zhai|inc.faer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['inc.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['inc.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['inc.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['inc.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incqing']) && !empty($param['incqing'])){
            $where[] = ['inc.incqing','=', $param['incqing']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['inc.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['inc.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['inc.baner','like', '%'.$param['baner'].'%'];
        }


        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $data['data'] = IncomeEnginModel::alias('inc')
            ->field('inc.*,acc.name as accname,acc.accnum')
            ->leftJoin('account acc','acc.id=inc.accid')
            ->where($where)
            ->order('inc.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_wen_ban(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ban.deleted_user','=', 0];
        $where[] = ['ban.comid','=',$this->userinfo['comid']];
        $where[] = ['ban.isturn','=',1];
        $where[] = ['ban.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ban.title|ban.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['wentype']) && !empty($param['wentype'])){
            $where[] = ['ban.wentype','=', $param['wentype']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['ban.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['wenzi']) && !empty($param['wenzi'])){
            $where[] = ['ban.wenzi','=', $param['wenzi']];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ban.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['wenlevel']) && !empty($param['wenlevel'])){
            $where[] = ['ban.wenlevel','=', $param['wenlevel']];
        }
        if(isset($param['wenjin']) && !empty($param['wenjin'])){
            $where[] = ['ban.wenjin','=', $param['wenjin']];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['ban.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['zhuti']) && !empty($param['zhuti'])){
            $where[] = ['ban.zhuti','like', '%'.$param['zhuti'].'%'];
        }



        $data['wentype'] = (new OptService())->get_base_info('qi_wen_wentype');
        $data['is_wentype'] = isset($param['wentype']) ? $param['wentype'] : '';

        $data['wenzi'] = (new OptService())->get_base_info('qi_wen_wenzi');
        $data['is_wenzi'] = isset($param['wenzi']) ? $param['wenzi'] : '';

        $data['wenlevel'] = (new OptService())->get_base_info('qi_wen_wenlevel');
        $data['is_wenlevel'] = isset($param['wenlevel']) ? $param['wenlevel'] : '';

        $data['wenjin'] = (new OptService())->get_base_info('qi_wen_wenjin');
        $data['is_wenjin'] = isset($param['wenjin']) ? $param['wenjin'] : '';

        $data['data'] = WenBanModel::alias('ban')
            ->field('ban.*')
            ->where($where)
            ->where((new MenuService())->get_opt_where('wen_ban', 'ban'))
            ->order('ban.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['wentype'] = (new OptService())->get_base_showname('qi_wen_wentype', $item['wentype']);
                $item['wenzi'] = (new OptService())->get_base_showname('qi_wen_wenzi', $item['wenzi']);
                $item['wenlevel'] = (new OptService())->get_base_showname('qi_wen_wenlevel', $item['wenlevel']);
                $item['wenjin'] = (new OptService())->get_base_showname('qi_wen_wenjin', $item['wenjin']);

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_wen_fa(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['fa.deleted_user','=', 0];
        $where[] = ['fa.comid','=',$this->userinfo['comid']];
        $where[] = ['fa.isturn','=',1];
        $where[] = ['fa.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ban.title|ban.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['wentype']) && !empty($param['wentype'])){
            $where[] = ['ban.wentype','=', $param['wentype']];
        }
        if(isset($param['wenzi']) && !empty($param['wenzi'])){
            $where[] = ['ban.wenzi','=', $param['wenzi']];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ban.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['wenlevel']) && !empty($param['wenlevel'])){
            $where[] = ['ban.wenlevel','=', $param['wenlevel']];
        }
        if(isset($param['wenjin']) && !empty($param['wenjin'])){
            $where[] = ['ban.wenjin','=', $param['wenjin']];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['ban.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['zhuti']) && !empty($param['zhuti'])){
            $where[] = ['ban.zhuti','like', '%'.$param['zhuti'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['fa.dt','between', explode(' - ', $param['dt'])];
        }


        $data['wentype'] = (new OptService())->get_base_info('qi_wen_wentype');
        $data['is_wentype'] = isset($param['wentype']) ? $param['wentype'] : '';

        $data['wenzi'] = (new OptService())->get_base_info('qi_wen_wenzi');
        $data['is_wenzi'] = isset($param['wenzi']) ? $param['wenzi'] : '';

        $data['wenlevel'] = (new OptService())->get_base_info('qi_wen_wenlevel');
        $data['is_wenlevel'] = isset($param['wenlevel']) ? $param['wenlevel'] : '';

        $data['wenjin'] = (new OptService())->get_base_info('qi_wen_wenjin');
        $data['is_wenjin'] = isset($param['wenjin']) ? $param['wenjin'] : '';

        $data['data'] = WenFaModel::alias('fa')
            ->field('fa.*,ban.wentype,ban.wenzi,ban.number,ban.wenlevel,ban.wenjin,ban.title,ban.zhuti,ban.zhudept')
            ->join('wen_ban ban','ban.id=fa.banid')
            ->where($where)
            ->where((new MenuService())->get_opt_where('wen_ban', 'ban'))
            ->order('fa.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['wentype'] = (new OptService())->get_base_showname('qi_wen_wentype', $item['wentype']);
                $item['wenzi'] = (new OptService())->get_base_showname('qi_wen_wenzi', $item['wenzi']);
                $item['wenlevel'] = (new OptService())->get_base_showname('qi_wen_wenlevel', $item['wenlevel']);
                $item['wenjin'] = (new OptService())->get_base_showname('qi_wen_wenjin', $item['wenjin']);
                $item['zhudept_name'] = DeptModel::get_name_by_ids($item['zhudept']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_wen_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['shou.title|shou.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['wenlevel']) && !empty($param['wenlevel'])){
            $where[] = ['shou.wenlevel','=', $param['wenlevel']];
        }
        if(isset($param['wenjin']) && !empty($param['wenjin'])){
            $where[] = ['shou.wenjin','=', $param['wenjin']];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['shou.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['zhuti']) && !empty($param['zhuti'])){
            $where[] = ['shou.zhuti','like', '%'.$param['zhuti'].'%'];
        }

        $data['wenlevel'] = (new OptService())->get_base_info('qi_wen_wenlevel');
        $data['is_wenlevel'] = isset($param['wenlevel']) ? $param['wenlevel'] : '';

        $data['wenjin'] = (new OptService())->get_base_info('qi_wen_wenjin');
        $data['is_wenjin'] = isset($param['wenjin']) ? $param['wenjin'] : '';

        $data['data'] = WenShouModel::alias('shou')
            ->field('shou.*')
            ->where($where)
            ->where((new MenuService())->get_opt_where('wen_shou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['wenlevel'] = (new OptService())->get_base_showname('qi_wen_wenlevel', $item['wenlevel']);
                $item['wenjin'] = (new OptService())->get_base_showname('qi_wen_wenjin', $item['wenjin']);
                $item['deptid_name'] = DeptModel::get_name_by_pk($item['deptid']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }



    public function get_qian_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','=',1];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['shou.bidid','=', $param['bidid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['b.name|shou.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['bidname']) && !empty($param['bidname'])){
            $where[] = ['b.name','like', '%'.$param['bidname'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['shou.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }


        $data['data'] = QianShouModel::alias('shou')
            ->field('shou.*,b.name as bidname,b.number as bidnumber,b.state as bidstate,b.status as bidstatus
            ,b.gofuer,b.gui_unit,acc.name as accname,acc.accnum')
            ->join('bid b','b.id=shou.bidid')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('qian_shou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['accid_name'] = $item['accname'].'('.$item['accnum'].')';
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen(['status'=>$item['bidstatus'],'state'=>$item['bidstate']]);
                $item['gui_unit_name'] = DeptModel::where(['id'=>$item['gui_unit']])->value('name');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_tou_jiao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jiao.deleted_user','=', 0];
        $where[] = ['jiao.comid','=',$this->userinfo['comid']];
        $where[] = ['jiao.isturn','=',1];
        $where[] = ['jiao.status','=',1];

        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['jiao.bidid','=', $param['bidid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['b.name|jiao.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['bidname']) && !empty($param['bidname'])){
            $where[] = ['b.name','like', '%'.$param['bidname'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['jiao.zhai','like', '%'.$param['zhai'].'%'];
        }

        if(isset($param['ziqu']) && !empty($param['ziqu'])){
            $where[] = ['jiao.ziqu','=', $param['ziqu']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jiao.status','=', $param['status']];
        }


        $data['ziqu'] = (new QianService())->get_tou_jiao_ziqu();
        $data['is_ziqu'] = isset($param['ziqu']) ? $param['ziqu'] : '';

        $data['data'] = TouJiaoModel::alias('jiao')
            ->field('jiao.*,b.name as bidname,b.number as bidnumber,b.state as bidstate,b.status as bidstatus
            ,b.gofuer,b.gui_unit')
            ->join('bid b','b.id=jiao.bidid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('tou_jiao', 'jiao'))
            ->order('jiao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen(['status'=>$item['bidstatus'],'state'=>$item['bidstate']]);
                $item['gui_unit_name'] = DeptModel::where(['id'=>$item['gui_unit']])->value('name');

                $item['zhiqilei_name'] = (new OptService())->get_base_showname('cai_zhiqilei', $item['zhiqilei']);
                $item['ziqu_name'] = (new QianService())->get_tou_jiao_ziqu_name($item['ziqu']);

                $item['shouid_name'] = '';
                $shouidInfo = QianShouModel::where(['id'=>$item['shouid']])->find();
                if($shouidInfo){
                    $item['shouid_name'] = $shouidInfo['zhai'].$shouidInfo['money'];
                }

                $item['jiaosericnum'] = FlowBillModel::where(['modenum'=>'tou_jiao','mid'=>$item['id']])->value('sericnum');
                $item['onefuer'] = TouJiaoListModel::where(['jiaoid'=>$item['id']])->value('fuer');

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }



    // 投标
    public function get_bid(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['jiaer']) && !empty($param['jiaer'])){
            $where[] = ['bid.jiaer','like', '%'.$param['jiaer'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['bid.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['jinglei']) && !empty($param['jinglei'])){
            $where[] = ['bid.jinglei','=', $param['jinglei']];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['tou_unit']) && !empty($param['tou_unit'])){
            $where[] = ['tou_unit.name','like', '%'.$param['tou_unit'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.name','like', '%'.$param['gui_unit'].'%'];
        }
        if(isset($param['lanshi']) && !empty($param['lanshi'])){
            $where[] = ['bid.lanshi','=', $param['lanshi']];
        }
        if(isset($param['zilai']) && !empty($param['zilai'])){
            $where[] = ['bid.zilai','=', $param['zilai']];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['bid.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['kaitype']) && !empty($param['kaitype'])){
            $where[] = ['bid.kaitype','=', $param['kaitype']];
        }
        if(isset($param['kailei']) && !empty($param['kailei'])){
            $where[] = ['bid.kailei','=', $param['kailei']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['zhao_unit']) && !empty($param['zhao_unit'])){
            $where[] = ['zhao_unit.name','like', '%'.$param['zhao_unit'].'%'];
        }
        if(isset($param['enddt_bao']) && !empty($param['enddt_bao'])){
            $where[] = ['bid.enddt_bao','between', explode(' - ', $param['enddt_bao'])];
        }
        if(isset($param['enddt_tou']) && !empty($param['enddt_tou'])){
            $where[] = ['bid.enddt_tou','between', explode(' - ', $param['enddt_tou'])];
        }
        if(isset($param['jinger_name']) && !empty($param['jinger_name'])){
            $where[] = ['jinger.realname','like', '%'.$param['jinger_name'].'%'];
        }
        if(isset($param['touer_name']) && !empty($param['touer_name'])){
            $where[] = ['touer.realname','like', '%'.$param['touer_name'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=', $param['state']];
        }
        if(isset($param['status']) && !empty($param['status'])){
            $where[] = ['bid.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['tou_unit'] = $data['chanid'];
        $data['is_tou_unit'] = isset($param['tou_unit']) ? $param['tou_unit'] : '';
        $data['gui_unit'] = $data['chanid'];
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
        $data['is_lanshi'] = isset($param['lanshi']) ? $param['lanshi'] : '';

        $data['zilai'] = (new OptService())->get_base_info('bid_zilai');
        $data['is_zilai'] = isset($param['zilai']) ? $param['zilai'] : '';

        $data['kaitype'] = (new OptService())->get_base_info('bid_kaitype');
        $data['is_kaitype'] = isset($param['kaitype']) ? $param['kaitype'] : '';

        $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
        $data['is_kailei'] = isset($param['kailei']) ? $param['kailei'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new BidService())->get_bid_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = BidModel::alias('bid')
            ->field('bid.*,tou_unit.name as tou_unit_name,gui_unit.name as gui_unit_name,
            jinger.realname as jinger_name,touer.realname as touer_name')
            ->leftJoin('dept tou_unit','tou_unit.id=bid.tou_unit')
            ->leftJoin('dept gui_unit','gui_unit.id=bid.gui_unit')
            ->leftJoin('user jinger','jinger.id=bid.jinger')
            ->leftJoin('user touer','touer.id=bid.touer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $item['lanshi']);
                $item['zilai_name'] = (new OptService())->get_base_showname('bid_zilai', $item['zilai']);
                $item['kaitype_name'] = (new OptService())->get_base_showname('bid_kaitype', $item['kaitype']);
                $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_zhaotype', $item['zhaotype']);

                $item['state_name'] = (new BidService())->get_bid_state_name_tag($item);
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen($item);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                $item['qian_count'] = (new QianService())->get_bid_qian_count($item['id']);


                $cert_use = [];
                $certuse = CertUseBidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'isreal'=>1,'bidid'=>$item['id']])->find();
                if($certuse){
                    $certuselist = CertUseBidListModel::where(['useid'=>$certuse['id']])->select()->toArray();
                    foreach ($certuselist as $vvv){
                        $certInfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        $vvv['name'] = $certInfo['name'];
                        $vvv['number'] = $certInfo['number'];
                        $vvv['major_name'] = str_replace('[]', ',', $certInfo['major']);
                        $vvv['person_name'] = CertPersonModel::where(['id'=>$certInfo['personid']])->value('name');
                        $vvv['statueuse_name'] = (new CertService())->get_cert_statueuse_name($certInfo['statueuse']);
                        $vvv['chanid_name'] = DeptModel::get_name_by_pk($certInfo['chanid']);
                        $cert_use[] = $vvv;
                    }

                }
                $item['cert_use'] = $cert_use;

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_bid_yin(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];
        $where[] = ['bid.state','=',3];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['jiaer']) && !empty($param['jiaer'])){
            $where[] = ['bid.jiaer','like', '%'.$param['jiaer'].'%'];
        }
        if(isset($param['gofuer']) && !empty($param['gofuer'])){
            $where[] = ['bid.gofuer','like', '%'.$param['gofuer'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['tou_unit']) && !empty($param['tou_unit'])){
            $where[] = ['tou_unit.name','like', '%'.$param['tou_unit'].'%'];
        }
        if(isset($param['gui_unit']) && !empty($param['gui_unit'])){
            $where[] = ['gui_unit.name','like', '%'.$param['gui_unit'].'%'];
        }
        if(isset($param['lanshi']) && !empty($param['lanshi'])){
            $where[] = ['bid.lanshi','=', $param['lanshi']];
        }
        if(isset($param['zilai']) && !empty($param['zilai'])){
            $where[] = ['bid.zilai','=', $param['zilai']];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['bid.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['kaitype']) && !empty($param['kaitype'])){
            $where[] = ['bid.kaitype','=', $param['kaitype']];
        }
        if(isset($param['kailei']) && !empty($param['kailei'])){
            $where[] = ['bid.kailei','=', $param['kailei']];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['zhao_unit']) && !empty($param['zhao_unit'])){
            $where[] = ['zhao_unit.name','like', '%'.$param['zhao_unit'].'%'];
        }
        if(isset($param['enddt_bao']) && !empty($param['enddt_bao'])){
            $where[] = ['bid.enddt_bao','between', explode(' - ', $param['enddt_bao'])];
        }
        if(isset($param['enddt_tou']) && !empty($param['enddt_tou'])){
            $where[] = ['bid.enddt_tou','between', explode(' - ', $param['enddt_tou'])];
        }
        if(isset($param['jinger_name']) && !empty($param['jinger_name'])){
            $where[] = ['jinger.realname','like', '%'.$param['jinger_name'].'%'];
        }
        if(isset($param['touer_name']) && !empty($param['touer_name'])){
            $where[] = ['touer.realname','like', '%'.$param['touer_name'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['tou_unit'] = $data['chanid'];
        $data['is_tou_unit'] = isset($param['tou_unit']) ? $param['tou_unit'] : '';
        $data['gui_unit'] = $data['chanid'];
        $data['is_gui_unit'] = isset($param['gui_unit']) ? $param['gui_unit'] : '';

        $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
        $data['is_lanshi'] = isset($param['lanshi']) ? $param['lanshi'] : '';

        $data['zilai'] = (new OptService())->get_base_info('bid_zilai');
        $data['is_zilai'] = isset($param['zilai']) ? $param['zilai'] : '';

        $data['kaitype'] = (new OptService())->get_base_info('bid_kaitype');
        $data['is_kaitype'] = isset($param['kaitype']) ? $param['kaitype'] : '';

        $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
        $data['is_kailei'] = isset($param['kailei']) ? $param['kailei'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_zhaotype');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        if(isset($param['islan']) && $param['islan'] != ''){
            if($param['islan'] == 1){
                $where[] = ['e.id','>', 0];
            }elseif($param['islan'] == 2){
                $where[] = ['e.id','=', null];
            }
        }

        $data['data'] = BidModel::alias('bid')
            ->field('bid.*,tou_unit.name as tou_unit_name,gui_unit.name as gui_unit_name,
            jinger.realname as jinger_name,touer.realname as touer_name,e.id as enginid')
            ->leftJoin('dept tou_unit','tou_unit.id=bid.tou_unit')
            ->leftJoin('dept gui_unit','gui_unit.id=bid.gui_unit')
            ->leftJoin('user jinger','jinger.id=bid.jinger')
            ->leftJoin('user touer','touer.id=bid.touer')
            ->leftJoin('engin e','e.bidid=bid.id and e.deleted_user=0 and e.isturn=1 and e.status in (0,1)')
            ->where($where)
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['hanghao'] = $key+1;
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['lanshi_name'] = (new OptService())->get_base_showname('bid_lanshi', $item['lanshi']);
                $item['zilai_name'] = (new OptService())->get_base_showname('bid_zilai', $item['zilai']);
                $item['kaitype_name'] = (new OptService())->get_base_showname('bid_kaitype', $item['kaitype']);
                $item['kailei_name'] = (new OptService())->get_base_showname('bid_kailei', $item['kailei']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_zhaotype', $item['zhaotype']);

                $item['state_name'] = (new BidService())->get_bid_state_name_wen($item);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);


                $item['gofutel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['name'=>$item['gofuer']])->value('liantel');

                $item['win_unit_info'] = (new UserService())->get_users_part_arr($item['tou_unit'], 'dept');
                $item['gui_unit_info'] = (new UserService())->get_users_part_arr($item['gui_unit'], 'dept');

//                $item['enginid'] = 0;
//                $exist_engin = EnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
//                    ->where(['isturn'=>1,'bidid'=>$item['id']])->where('status','in',[0,1])->find();
//                if($exist_engin){
//                    $item['enginid'] = $exist_engin['id'];
//                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_bid_page_certuse(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['bidtype']) && !empty($param['bidtype'])){
            $where[] = ['bid.bidtype','=', $param['bidtype']];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['zhaoname']) && !empty($param['zhaoname'])){
            $where[] = ['bid.zhaoname','like', '%'.$param['zhaoname'].'%'];
        }
        if(isset($param['zhaotel']) && !empty($param['zhaotel'])){
            $where[] = ['bid.zhaotel','like', '%'.$param['zhaotel'].'%'];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['bid.business','=', $param['business']];
        }
        if(isset($param['jingbeier']) && !empty($param['jingbeier'])){
            $where[] = ['bid.jingbeier','like', '%'.$param['jingbeier'].'%'];
        }
        if(isset($param['xinlai']) && !empty($param['xinlai'])){
            $where[] = ['bid.xinlai','like', '%'.$param['xinlai'].'%'];
        }
        if(isset($param['enddt_bao']) && !empty($param['enddt_bao'])){
            $where[] = ['bid.enddt_bao','between', explode(' - ', $param['enddt_bao'])];
        }
        if(isset($param['enddt_tou']) && !empty($param['enddt_tou'])){
            $where[] = ['bid.enddt_tou','between', explode(' - ', $param['enddt_tou'])];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['bid.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['touer_name']) && !empty($param['touer_name'])){
            $where[] = ['tou.realname','like', '%'.$param['touer_name'].'%'];
        }
        if(isset($param['touunit']) && !empty($param['touunit'])){
            $where[] = ['bid.touunit','=', $param['touunit']];
        }
        if(isset($param['toushuo']) && !empty($param['toushuo'])){
            $where[] = ['bid.toushuo','like', '%'.$param['toushuo'].'%'];
        }
        if(isset($param['lianunit']) && !empty($param['lianunit'])){
            $where[] = ['bid.lianunit','like', '%'.$param['lianunit'].'%'];
        }
        if(isset($param['isfei']) && !empty($param['isfei'])){
            $where[] = ['bid.isfei','=', $param['isfei']];
        }
        if(isset($param['feishuo']) && !empty($param['feishuo'])){
            $where[] = ['bid.feishuo','like', '%'.$param['feishuo'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=', $param['state']];
        }



        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['touunit'] = $data['chanid'];
        $data['is_touunit'] = isset($param['touunit']) ? $param['touunit'] : '';

        $data['bidtype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_bidtype'] = isset($param['bidtype']) ? $param['bidtype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new BidService())->get_bid_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        if(isset($param['apply_type'])){
            if($param['apply_type'] == 1){
                $ids = CertUseModel::where(['deleted_user'=>0,'type'=>1,'isturn'=>1])->where('status','in',[0,1])->column('enginid');
                $ids = array_unique($ids);
                $where[] = ['bid.id','not in',$ids];
            }else if($param['apply_type'] == 2){
                $ids = CertUseModel::where(['deleted_user'=>0,'type'=>1,'isturn'=>1,'apply_type'=>1,'status'=>1])->column('enginid');
                $ids = array_unique($ids);
                $where[] = ['bid.id','in',$ids];
            }
        }


        $data['data'] = BidModel::alias('bid')
            ->field('bid.*,tou.realname as touer_name')
            ->leftJoin('user tou','tou.id=bid.touer')
            ->where($where)
            ->where((new MenuService())->get_opt_where('bid', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['bidtype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['bidtype']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new BidService())->get_bid_state_name_tag($item);
                $item['touunit_name'] = DeptModel::where(['id'=>$item['touunit']])->value('name');
                $item['bianer_name'] = UserModel::get_realname_by_ids($item['bianer']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);


                // 获取用证信息
                $cert_use = CertUseListModel::alias('list')->field('list.*')
                    ->leftJoin('cert_use use','use.id = list.mid')
                    ->where(['use.enginid'=>$item['id'],'use.deleted_user'=>0,'use.type'=>1,'list.pid'=>0,'list.isdel'=>0,'list.temp_isdel'=>0])
                    ->select()->toArray();
                $use = CertModel::get_use();
                foreach ($cert_use as &$v){
                    $cert_data = CertModel::where('id',$v['cert_id'])->find();
                    $v['cert_name'] = $cert_data['name'];
                    $v['cert_number'] = $cert_data['number'];
                    $v['cert_major'] = str_replace('[]', ',', $cert_data['major']);
                    $v['person_name'] = CertPersonModel::where('id',$cert_data['personid'])->value('name');
                    $v['use_name'] = isset($use[$cert_data['use']]) ? $use[$cert_data['use']] : '';
                    $v['com_name'] = DeptModel::get_name_by_pk($cert_data['chanid']);
                    $v['issuo_name'] = $v['issuo'] == 1 ? '是' : '否';
                }
                $item['cert_use'] = $cert_use;

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_nei(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['nei.deleted_user','=', 0];
        $where[] = ['nei.comid','=',$this->userinfo['comid']];
        $where[] = ['nei.isturn','=',1];
        $where[] = ['nei.status','=',1];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['nei.name|nei.number|nei.dutyer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['neihetype']) && !empty($param['neihetype'])){
            $where[] = ['nei.neihetype','=', $param['neihetype']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['nei.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['nei.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['nei.year','=', $param['year']];
        }
        if(isset($param['dutyer']) && !empty($param['dutyer'])){
            $where[] = ['nei.dutyer','like', '%'.$param['dutyer'].'%'];
        }
        if(isset($param['dutytel']) && !empty($param['dutytel'])){
            $where[] = ['nei.dutytel','like', '%'.$param['dutytel'].'%'];
        }
        if(isset($param[' canal']) && !empty($param[' canal'])){
            $where[] = ['nei. canal','like', '%'.$param[' canal'].'%'];
        }
        if(isset($param['rate']) && !empty($param['rate'])){
            $where[] = ['nei.rate','like', '%'.$param['rate'].'%'];
        }
        if(isset($param['baofee']) && !empty($param['baofee'])){
            $where[] = ['nei.baofee','like', '%'.$param['baofee'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['nei.state','=', $param['state']];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['nei.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['nei.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['settle']) && !empty($param['settle'])){
            $where[] = ['nei.settle','like', '%'.$param['settle'].'%'];
        }
        if(isset($param['clause']) && !empty($param['clause'])){
            $where[] = ['nei.clause','like', '%'.$param['clause'].'%'];
        }
        if(isset($param['viola']) && !empty($param['viola'])){
            $where[] = ['nei.viola','like', '%'.$param['viola'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['nei.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new ContractService())->get_contract_nei_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['data'] = ContractNeiModel::alias('nei')
            ->field('nei.*')
            ->where($where)
            ->order('nei.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['neihetype_name'] = (new ContractService())->get_contract_neihetype_name($item['neihetype']);
                $item['state_name'] = (new ContractService())->get_contract_nei_state_name_tag($item['id'], $item['state']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_yang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['yang.deleted_user','=', 0];
        $where[] = ['yang.comid','=',$this->userinfo['comid']];


        if(isset($param['yangname']) && !empty($param['yangname'])){
            $where[] = ['yang.yangname','like', '%'.$param['yangname'].'%'];
        }
        if(isset($param['yangshuo']) && !empty($param['yangshuo'])){
            $where[] = ['yang.yangshuo','like', '%'.$param['yangshuo'].'%'];
        }

        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['yang.state','=', $param['state']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['yang.year','like', '%'.$param['year'].'%'];
        }
        if(isset($param['caihetype']) && !empty($param['caihetype'])){
            $where[] = ['yang.caihetype','=', $param['caihetype']];
        }
        if(isset($param['caiheshu']) && !empty($param['caiheshu'])){
            $where[] = ['yang.caiheshu','=', $param['caiheshu']];
        }
        if(isset($param['caier']) && !empty($param['caier'])){
            $where[] = ['yang.caier','=', $param['caier']];
        }
        if(isset($param['myer']) && !empty($param['myer'])){
            $where[] = ['yang.myer','like', '%'.$param['myer'].'%'];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['yang.qiandt','between', explode(' - ', $param['qiandt'])];
        }


        $chaninfo = (new UserService())->get_chaninfo_all();
        $data['caier'] = $chaninfo;
        $data['is_caier'] = isset($param['caier']) ? $param['caier'] : '';

        $data['state'] = (new ContractService())->get_contract_cai_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['caihetype'] = (new ContractService())->get_contract_caihetype();
        $data['is_caihetype'] = isset($param['caihetype']) ? $param['caihetype'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['caiheshu'] = (new OptService())->get_base_info('engin_caiheshu');
        $data['is_caiheshu'] = isset($param['caiheshu']) ? $param['caiheshu'] : '';



        $data['data'] = ContractYangModel::alias('yang')
            ->field('yang.*')
            ->where($where)
            ->where((new MenuService())->get_opt_where('contract_yang', 'yang'))
            ->order('yang.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caihetype_name'] = (new ContractService())->get_contract_caihetype_name($item['caihetype']);
//                $item['caiheshu'] = (new OptService())->get_base_showname('engin_caiheshu', $item['caiheshu']);
//                $item['caier_name'] = DeptModel::get_name_by_pk($item['caier']);
//                $item['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($item['id'], $item['state']);

                $item['caier_info'] = (new UserService())->get_users_part_arr($item['caier'], 'dept');
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


    public function get_contract_shi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shi.deleted_user','=', 0];
        $where[] = ['shi.comid','=',$this->userinfo['comid']];
        $where[] = ['shi.isturn','=',1];
        $where[] = ['shi.status','=',1];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|shi.name|shi.number|shi.faer|shi.money|shi.shimoney|shi.rate|shi.duration|shi.zhilevel|shi.fauser|shi.myer|shi.fatel|shi.zhirate|shi.zhifee|shi.zhinum','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shi.enginid','=', $param['enginid']];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['shi.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shi.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shihetype']) && !empty($param['shihetype'])){
            $where[] = ['shi.shihetype','=', $param['shihetype']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['shi.year','like', '%'.$param['year'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['shi.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['chenger']) && !empty($param['chenger'])){
            $where[] = ['shi.chenger','=', $param['chenger']];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['shi.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['shimoney']) && !empty($param['shimoney'])){
            $where[] = ['shi.shimoney','like', '%'.$param['shimoney'].'%'];
        }
        if(isset($param['rate']) && !empty($param['rate'])){
            $where[] = ['shi.rate','like', '%'.$param['rate'].'%'];
        }
        if(isset($param['ratefee']) && !empty($param['ratefee'])){
            $where[] = ['shi.ratefee','like', '%'.$param['ratefee'].'%'];
        }
        if(isset($param['noratefee']) && !empty($param['noratefee'])){
            $where[] = ['shi.noratefee','like', '%'.$param['noratefee'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['shi.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['shi.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['duration']) && !empty($param['duration'])){
            $where[] = ['shi.duration','like', '%'.$param['duration'].'%'];
        }
        if(isset($param['zhilevel']) && !empty($param['zhilevel'])){
            $where[] = ['shi.zhilevel','like', '%'.$param['zhilevel'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['shi.state','=', $param['state']];
        }
        if(isset($param['fauser']) && !empty($param['fauser'])){
            $where[] = ['shi.fauser','like', '%'.$param['fauser'].'%'];
        }
        if(isset($param['myer']) && !empty($param['myer'])){
            $where[] = ['shi.myer','like', '%'.$param['myer'].'%'];
        }
        if(isset($param['fatel']) && !empty($param['fatel'])){
            $where[] = ['shi.fatel','like', '%'.$param['fatel'].'%'];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['shi.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['zhirate']) && !empty($param['zhirate'])){
            $where[] = ['shi.zhirate','like', '%'.$param['zhirate'].'%'];
        }
        if(isset($param['zhifee']) && !empty($param['zhifee'])){
            $where[] = ['shi.zhifee','like', '%'.$param['zhifee'].'%'];
        }
        if(isset($param['zhinum']) && !empty($param['zhinum'])){
            $where[] = ['shi.zhinum','like', '%'.$param['zhinum'].'%'];
        }
        if(isset($param['zhidt']) && !empty($param['zhidt'])){
            $where[] = ['shi.zhidt','between', explode(' - ', $param['zhidt'])];
        }


        $chaninfo = (new UserService())->get_chaninfo_all();

        $data['chenger'] = $chaninfo;
        $data['is_chenger'] = isset($param['chenger']) ? $param['chenger'] : '';

        $data['state'] = (new ContractService())->get_contract_shi_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
        $data['is_shihetype'] = isset($param['shihetype']) ? $param['shihetype'] : '';

        $data['data'] = ContractShiModel::alias('shi')
            ->field('shi.*,e.name as enginname')
            ->join('engin e','e.id=shi.enginid')
            ->where($where)
            ->order('shi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chenger_name'] = DeptModel::get_name_by_pk($item['chenger']);
                $item['shihetype'] = (new OptService())->get_base_showname('engin_shihetype', $item['shihetype']);
                $item['state_name'] = (new ContractService())->get_contract_shi_state_name_tag($item['id'], $item['state']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_shi_gong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];
        $where[] = ['isturn','=',1];
        $where[] = ['status','=',1];
        $where[] = ['enginid','=',$param['enginid']];

        $shiid = ContractShiModel::where($where)->value('id');
        $buidArr = [];
        if($shiid){
            $buidArr = ContractBuModel::where(['deleted_user'=>0,'comid'=>$this->userinfo['comid']])
                ->where(['status'=>1,'isturn'=>1,'enginid'=>$param['enginid'],'shiid'=>$shiid])->column('id');
        }

        $whereSou = "shiid=".$shiid;
        if($buidArr){
            $buStr = '('.implode(',', $buidArr).')';
            $whereSou .= " or buid in ".$buStr;
        }

        $where222 = [];
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where222[] = ['num|name|xiang','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['num']) && !empty($param['num'])){
            $where222[] = ['num','like', '%'.$param['num'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where222[] = ['name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xiang']) && !empty($param['xiang'])){
            $where222[] = ['xiang','like', '%'.$param['xiang'].'%'];
        }

        $list = ContractShiGongModel::where($whereSou)->where($where222)->select()->toArray();
        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_contract_cai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['cai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|cai.name|cai.number|cai.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['cai.state','=', $param['state']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['cai.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['caitype']) && !empty($param['caitype'])){
            $where[] = ['cai.caitype','=', $param['caitype']];
        }
        if(isset($param['hetype']) && !empty($param['hetype'])){
            $where[] = ['cai.hetype','=', $param['hetype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['cai.qiandt','between', explode(' - ', $param['qiandt'])];
        }

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['cai.status','=', $param['status']];
        }


        $data['caier'] = (new UserService())->get_chaninfo_all();
        $data['is_caier'] = isset($param['caier']) ? $param['caier'] : '';

        $data['caitype'] = (new OptService())->get_base_info('engin_cai_caitype');
        $data['is_caitype'] = isset($param['caitype']) ? $param['caitype'] : '';

        $data['state'] = (new ContractService())->get_contract_cai_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['hetype'] = (new ContractService())->get_contract_hetype();
        $data['is_hetype'] = isset($param['hetype']) ? $param['hetype'] : '';

        $data['data'] = ContractCaiModel::alias('cai')
            ->field('cai.*,e.name as enginname')
            ->join('engin e','e.id=cai.enginid')
            ->where($where)
            ->order('cai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caitype_name'] = (new OptService())->get_base_showname('engin_cai_caitype', $item['caitype']);
                $item['hetype_name'] = (new ContractService())->get_contract_hetype_name($item['hetype']);
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);

                $item['caier_name'] = DeptModel::get_name_by_pk($item['caier']);
                $item['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($item['id'], $item['state']);

                $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                    ->where('status','in',[0,1])->find();



                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_chu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['chu.deleted_user','=', 0];
        $where[] = ['chu.comid','=',$this->userinfo['comid']];
        $where[] = ['chu.isturn','=',1];
        $where[] = ['chu.status','=',1];



        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['chu.name|chu.number|chu.gonger|chu.lianer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['chu.state','=', $param['state']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['chu.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['chu.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['caitype']) && !empty($param['caitype'])){
            $where[] = ['chu.caitype','=', $param['caitype']];
        }
        if(isset($param['hetype']) && !empty($param['hetype'])){
            $where[] = ['chu.hetype','=', $param['hetype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['chu.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['chu.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['caier']) && $param['caier'] != ''){
            $where[] = ['chu.caier','=', $param['caier']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['chu.status','=', $param['status']];
        }

        if(isset($param['lianer_zhi']) && !empty($param['lianer_zhi'])){
            $where[] = ['chu.lianer','=', $param['lianer_zhi']];
        }


        $data['caier'] = (new UserService())->get_chaninfo_all();
        $data['is_caier'] = isset($param['caier']) ? $param['caier'] : '';

        $data['caitype'] = (new OptService())->get_base_info('engin_cai_caitype');
        $data['is_caitype'] = isset($param['caitype']) ? $param['caitype'] : '';

        $data['state'] = (new ContractService())->get_contract_cai_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['hetype'] = (new ContractService())->get_contract_hetype();
        $data['is_hetype'] = isset($param['hetype']) ? $param['hetype'] : '';

        $data['data'] = ContractChuModel::alias('chu')
            ->field('chu.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_chu', 'chu'))
            ->order('chu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caitype_name'] = (new OptService())->get_base_showname('engin_cai_caitype', $item['caitype']);
                $item['hetype_name'] = (new ContractService())->get_contract_hetype_name($item['hetype']);
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);

                $item['caier_name'] = DeptModel::get_name_by_pk($item['caier']);
                $item['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($item['id'], $item['state']);




                //已付款累计
                $item['lei_kuan'] = HouChujinListModel::alias('list')
                    ->join('hou_chujin hou','hou.id=list.chujinid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.chuid'=>$item['id']])
                    ->sum('list.money');
                //已收票累计
                $item['lei_piao'] = PiaoChuListModel::alias('list')
                    ->join('piao_chu hou','hou.id=list.piaochuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.chuid'=>$item['id']])
                    ->sum('list.money');
                //已付税款累计
                $item['lei_shui'] = HouChushuiListModel::alias('list')
                    ->join('hou_chushui hou','hou.id=list.chushuiid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.fuer'=>$item['lianer']])
                    ->sum('list.money');
                //已签储备合同累计
                $item['lei_chumoney'] = ContractChuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'id'=>$item['id']])
                    ->sum('money');

                $item['dealcomlist'] = [];
                $gongerInfo = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'name'=>$item['gonger']])->find();
                if($gongerInfo){
                    $item['dealcomlist'] = DealcomListModel::where(['dealcomid'=>$gongerInfo['id']])->order('isdea DESC')->find();
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_cai_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['caiid','=',$param['caiid']];
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }


        $data['data'] = ContractCaiListModel::where($where)
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['subid'] = 0;
                $item['subname'] = '';
                $item['guiname'] = '';
                $listinfo = EnginBudgetListModel::where(['id'=>$item['listid']])->find();
                if($listinfo){
                    $item['subid'] = $listinfo['subid'];
                    $item['subname'] = (new BudgetService())->get_subid_quanname($listinfo['subid']);
                    $item['guiname'] = $listinfo['name'];
                }

                $item['hanghao'] = $key+1;
                $item['runum'] =  StuffRuListModel::alias('list')
                    ->join('stuff_ru ru','ru.id=list.ruid')
                    ->where(['ru.deleted_user'=>0,'ru.isturn'=>1])
                    ->where('ru.status','in',[0,1])
                    ->where('ru.ishe','=',1)
                    ->where('list.listid','=',$item['id'])
                    ->sum('list.num');
                $item['sheng_runum'] = round($item['num'] - $item['runum']);
                $item['rumoney'] =  StuffRuListModel::alias('list')
                    ->join('stuff_ru ru','ru.id=list.ruid')
                    ->where(['ru.deleted_user'=>0,'ru.isturn'=>1])
                    ->where('ru.ishe','=',1)
                    ->where('ru.status','in',[0,1])
                    ->where('list.listid','=',$item['id'])
                    ->sum('list.money');


                $item['labour_shennum'] =  LabourShenListModel::alias('list')
                    ->join('labour_shen shen','shen.id=list.shenid')
                    ->where(['shen.deleted_user'=>0,'shen.isturn'=>1])
                    ->where('shen.status','in',[0,1])
                    ->where('list.listid','=',$item['id'])
                    ->sum('shennum');
                $item['labour_henum'] =  LabourShenListModel::alias('list')
                    ->join('labour_shen shen','shen.id=list.shenid')
                    ->where(['shen.deleted_user'=>0,'shen.isturn'=>1])
                    ->where('shen.status','in',[0,1])
                    ->where('list.listid','=',$item['id'])
                    ->sum('henum');
                $item['subbao_shennum'] =  SubbaoShenListModel::alias('list')
                    ->join('subbao_shen shen','shen.id=list.shenid')
                    ->where(['shen.deleted_user'=>0,'shen.isturn'=>1])
                    ->where('shen.status','in',[0,1])
                    ->where('list.listid','=',$item['id'])
                    ->sum('shennum');
                $item['subbao_henum'] =  SubbaoShenListModel::alias('list')
                    ->join('subbao_shen shen','shen.id=list.shenid')
                    ->where(['shen.deleted_user'=>0,'shen.isturn'=>1])
                    ->where('shen.status','in',[0,1])
                    ->where('list.listid','=',$item['id'])
                    ->sum('henum');

                $item['jinnum'] =  DeviceJinListModel::alias('list')
                    ->join('device_jin jin','jin.id=list.jinid')
                    ->where(['jin.deleted_user'=>0,'jin.isturn'=>1])
                    ->where('jin.status','in',[0,1])
                    ->where('list.listid','=',$item['id'])
                    ->count();
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_cai_page_subbaojie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];
        $where[] = ['cai.caihetype','=',4];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.name|cai.number|cai.gonger|cai.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['cai.enginid','=', $param['enginid']];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['cai.state','=', $param['state']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['cai.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }

        if(isset($param['caiheshu']) && !empty($param['caiheshu'])){
            $where[] = ['cai.caiheshu','=', $param['caiheshu']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['cai.year','like', '%'.$param['year'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['caier']) && !empty($param['caier'])){
            $where[] = ['cai.caier','=', $param['caier']];
        }
        if(isset($param['gonguser']) && !empty($param['gonguser'])){
            $where[] = ['cai.gonguser','like', '%'.$param['gonguser'].'%'];
        }
        if(isset($param['myer']) && !empty($param['myer'])){
            $where[] = ['cai.myer','like', '%'.$param['myer'].'%'];
        }
        if(isset($param['gongtel']) && !empty($param['gongtel'])){
            $where[] = ['cai.gongtel','like', '%'.$param['gongtel'].'%'];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['cai.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['cai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['rate']) && !empty($param['rate'])){
            $where[] = ['cai.rate','like', '%'.$param['rate'].'%'];
        }
        if(isset($param['ratefee']) && !empty($param['ratefee'])){
            $where[] = ['cai.ratefee','like', '%'.$param['ratefee'].'%'];
        }
        if(isset($param['noratefee']) && !empty($param['noratefee'])){
            $where[] = ['cai.noratefee','like', '%'.$param['noratefee'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['cai.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['cai.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['zhirate']) && !empty($param['zhirate'])){
            $where[] = ['cai.zhirate','like', '%'.$param['zhirate'].'%'];
        }
        if(isset($param['zhifee']) && !empty($param['zhifee'])){
            $where[] = ['cai.zhifee','like', '%'.$param['zhifee'].'%'];
        }


        $chaninfo = (new UserService())->get_chaninfo_all();
        $data['caier'] = $chaninfo;
        $data['is_caier'] = isset($param['caier']) ? $param['caier'] : '';

        $data['state'] = (new ContractService())->get_contract_cai_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['caiheshu'] = (new OptService())->get_base_info('engin_caiheshu');
        $data['is_caiheshu'] = isset($param['caiheshu']) ? $param['caiheshu'] : '';




        $list = ContractCaiModel::alias('cai')
            ->field('cai.*,e.name as enginname')
            ->join('engin e','e.id=cai.enginid')
            ->where($where)
            ->order('cai.id DESC')
            ->select()->toArray();
        $info = [];
        foreach ($list as $item){
            $exist = SubbaoJieModel::where(['deleted_user'=>0,'isturn'=>1,'caiid'=>$item['id']])
                ->where('status','in',[0,1])->find();
            if(!$exist){
                $item['caihetype_name'] = (new ContractService())->get_contract_caihetype_name($item['caihetype']);
                $item['caiheshu'] = (new OptService())->get_base_showname('engin_caiheshu', $item['caiheshu']);
                $item['caier_name'] = DeptModel::get_name_by_pk($item['caier']);
                $item['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($item['id'], $item['state']);
                $info[] = $item;
            }
        }

        $data['data'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_cai_page_stuffru(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];
        $where[] = ['cai.caihetype','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.name|cai.number|cai.gonger|cai.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['cai.enginid','=', $param['enginid']];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['cai.state','=', $param['state']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['cai.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }

        if(isset($param['caiheshu']) && !empty($param['caiheshu'])){
            $where[] = ['cai.caiheshu','=', $param['caiheshu']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['cai.year','like', '%'.$param['year'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['caier']) && !empty($param['caier'])){
            $where[] = ['cai.caier','=', $param['caier']];
        }
        if(isset($param['gonguser']) && !empty($param['gonguser'])){
            $where[] = ['cai.gonguser','like', '%'.$param['gonguser'].'%'];
        }
        if(isset($param['myer']) && !empty($param['myer'])){
            $where[] = ['cai.myer','like', '%'.$param['myer'].'%'];
        }
        if(isset($param['gongtel']) && !empty($param['gongtel'])){
            $where[] = ['cai.gongtel','like', '%'.$param['gongtel'].'%'];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['cai.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['cai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['rate']) && !empty($param['rate'])){
            $where[] = ['cai.rate','like', '%'.$param['rate'].'%'];
        }
        if(isset($param['ratefee']) && !empty($param['ratefee'])){
            $where[] = ['cai.ratefee','like', '%'.$param['ratefee'].'%'];
        }
        if(isset($param['noratefee']) && !empty($param['noratefee'])){
            $where[] = ['cai.noratefee','like', '%'.$param['noratefee'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['cai.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['cai.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['zhirate']) && !empty($param['zhirate'])){
            $where[] = ['cai.zhirate','like', '%'.$param['zhirate'].'%'];
        }
        if(isset($param['zhifee']) && !empty($param['zhifee'])){
            $where[] = ['cai.zhifee','like', '%'.$param['zhifee'].'%'];
        }


        $chaninfo = (new UserService())->get_chaninfo_all();
        $data['caier'] = $chaninfo;
        $data['is_caier'] = isset($param['caier']) ? $param['caier'] : '';

        $data['state'] = (new ContractService())->get_contract_cai_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['caiheshu'] = (new OptService())->get_base_info('engin_caiheshu');
        $data['is_caiheshu'] = isset($param['caiheshu']) ? $param['caiheshu'] : '';




        $contractCai = ContractCaiModel::alias('cai')
            ->field('cai.*,e.name as enginname')
            ->join('engin e','e.id=cai.enginid')
            ->where($where)
            ->order('cai.id DESC')
            ->select()->toArray();
        $info = [];
        foreach ($contractCai as $item){
            $henum = 0;
            $runum = 0;
            $list = ContractCaiListModel::field('id,num')->where(['caiid'=>$item['id']])->select()->toArray();
            foreach ($list as $vvv){
                $henum += $vvv['num'];
                $runum += StuffRuListModel::alias('list')
                    ->join('stuff_ru ff','ff.id=list.ruid')
                    ->where(['ff.comid'=>$this->userinfo['comid'],'ff.deleted_user'=>0])
                    ->where(['ff.isturn'=>1,'ff.status'=>1])
                    ->where(['ff.ishe'=>1])
                    ->where(['list.listid'=>$vvv['id']])
                    ->sum('num');
            }


            if($runum < $henum){
                $item['caihetype_name'] = (new ContractService())->get_contract_caihetype_name($item['caihetype']);
                $item['caiheshu'] = (new OptService())->get_base_showname('engin_caiheshu', $item['caiheshu']);
                $item['caier_name'] = DeptModel::get_name_by_pk($item['caier']);
                $item['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($item['id'], $item['state']);
                $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                    ->where('status','in',[0,1])->find();
                if($deal){
                    $item['banknum'] = $deal['banknum'];
                    $item['bankname'] = $deal['bankname'];
                }

                $info[] = $item;
            }
        }

        $data['data'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_cai_page_labourjie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];
        $where[] = ['cai.caihetype','=',2];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['cai.enginid','=', $param['enginid']];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['cai.state','=', $param['state']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['cai.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }

        if(isset($param['caiheshu']) && !empty($param['caiheshu'])){
            $where[] = ['cai.caiheshu','=', $param['caiheshu']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['cai.year','like', '%'.$param['year'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['caier']) && !empty($param['caier'])){
            $where[] = ['cai.caier','=', $param['caier']];
        }
        if(isset($param['gonguser']) && !empty($param['gonguser'])){
            $where[] = ['cai.gonguser','like', '%'.$param['gonguser'].'%'];
        }
        if(isset($param['myer']) && !empty($param['myer'])){
            $where[] = ['cai.myer','like', '%'.$param['myer'].'%'];
        }
        if(isset($param['gongtel']) && !empty($param['gongtel'])){
            $where[] = ['cai.gongtel','like', '%'.$param['gongtel'].'%'];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['cai.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['cai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['rate']) && !empty($param['rate'])){
            $where[] = ['cai.rate','like', '%'.$param['rate'].'%'];
        }
        if(isset($param['ratefee']) && !empty($param['ratefee'])){
            $where[] = ['cai.ratefee','like', '%'.$param['ratefee'].'%'];
        }
        if(isset($param['noratefee']) && !empty($param['noratefee'])){
            $where[] = ['cai.noratefee','like', '%'.$param['noratefee'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['cai.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['cai.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['zhirate']) && !empty($param['zhirate'])){
            $where[] = ['cai.zhirate','like', '%'.$param['zhirate'].'%'];
        }
        if(isset($param['zhifee']) && !empty($param['zhifee'])){
            $where[] = ['cai.zhifee','like', '%'.$param['zhifee'].'%'];
        }


        $chaninfo = (new UserService())->get_chaninfo_all();
        $data['caier'] = $chaninfo;
        $data['is_caier'] = isset($param['caier']) ? $param['caier'] : '';

        $data['state'] = (new ContractService())->get_contract_cai_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['caiheshu'] = (new OptService())->get_base_info('engin_caiheshu');
        $data['is_caiheshu'] = isset($param['caiheshu']) ? $param['caiheshu'] : '';




        $list = ContractCaiModel::alias('cai')
            ->field('cai.*,e.name as enginname')
            ->join('engin e','e.id=cai.enginid')
            ->where($where)
            ->order('cai.id DESC')
            ->select()->toArray();
        $info = [];
        foreach ($list as $item){
            $exist = LabourJieModel::where(['deleted_user'=>0,'isturn'=>1,'caiid'=>$item['id']])
                ->where('status','in',[0,1])->find();
            if(!$exist){
                $item['caihetype_name'] = (new ContractService())->get_contract_caihetype_name($item['caihetype']);
                $item['caiheshu'] = (new OptService())->get_base_showname('engin_caiheshu', $item['caiheshu']);
                $item['caier_name'] = DeptModel::get_name_by_pk($item['caier']);
                $item['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($item['id'], $item['state']);
                $info[] = $item;
            }
        }

        $data['data'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }



    public function get_piao_chu_list(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['chu.deleted_user','=', 0];
        $where[] = ['chu.comid','=',$this->userinfo['comid']];
        $where[] = ['chu.isturn','=',1];
        $where[] = ['chu.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['chu.lianer|list.xiaoer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['chu.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['xiaoer']) && !empty($param['xiaoer'])){
            $where[] = ['list.xiaoer','like', '%'.$param['xiaoer'].'%'];
        }
        if(isset($param['qianpiaotype']) && $param['qianpiaotype'] != ''){
            $where[] = ['list.qianpiaotype','=', $param['qianpiaotype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['chu.status','=', $param['status']];
        }

        $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
        $data['is_qianpiaotype'] = isset($param['qianpiaotype']) ? $param['qianpiaotype'] : '';


        $existIdArr = ContractChuListModel::alias('list')
            ->leftJoin('contract_chu con','con.id=list.chuid')
            ->where(['con.comid'=>$this->userinfo['comid'],'con.deleted_user'=>0,'con.isturn'=>1])
            ->where('con.status','in',[0,1])
            ->group('list.id')
            ->column('list.id');
        if(!$existIdArr){
            $existIdArr = [0];
        }


        $data['data'] = PiaoChuListModel::alias('list')
            ->field('chu.*,
            list.id as listid,list.xiaoer,list.qianpiaotype,list.nums,list.kaidt,list.cailei,
            list.fee,list.rate,list.ratefee as listratefee,list.money as listmoney,list.huidt,list.explain as listexplain,
            list.difee,list.usedt')
            ->join('piao_chu chu','chu.id=list.piaochuid')
            ->where($where)
            ->where('list.id','not in',$existIdArr)
            ->order('chu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $item['qianpiaotype']);

                $item['yifufee'] = HouChujinGuanModel::alias('guan')
                    ->field('list.*')
                    ->join('hou_chujin jin','jin.id=guan.chujinid')
                    ->join('piao_chu piao','piao.id=guan.piaoid')
                    ->where(['jin.comid'=>$this->userinfo['comid'],'jin.deleted_user'=>0,'jin.isturn'=>1,'jin.status'=>1])
                    ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                    ->where(['guan.piaolistid'=>$item['listid']])
                    ->sum('difee');
                if($item['yifufee'] <= 0){
                    $item['statename'] = '未付款';
                }else{
                    if($item['yifufee'] < $item['listmoney']){
                        $item['statename'] = '部分付款';
                    }else{
                        $item['statename'] = '已付款';
                    }
                }
                $item['daifufee'] = round($item['listmoney'] - $item['yifufee'], 2);

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function get_labour_shen(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shen.deleted_user','=', 0];
        $where[] = ['shen.comid','=',$this->userinfo['comid']];
        $where[] = ['shen.isturn','=',1];
        $where[] = ['shen.status','=',1];
        $where[] = ['shen.enginid','=',$param['enginid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.name|cai.number|shen.number|shen.baner|cai.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shen.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shenmoney']) && !empty($param['shenmoney'])){
            $where[] = ['shen.shenmoney','like', '%'.$param['shenmoney'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['shen.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shen.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['shen.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }


        $data['data'] = LabourShenModel::alias('shen')
            ->field('shen.*,e.name as enginname,cai.name as cainame,cai.gonger')
            ->join('engin e','e.id=shen.enginid')
            ->join('contract_cai cai','cai.id=shen.caiid')
            ->where($where)
            ->order('shen.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                    ->where('status','in',[0,1])->find();
                if($deal){
                    $item['banknum'] = $deal['banknum'];
                    $item['bankname'] = $deal['bankname'];
                }

                $item['qingfee'] = (new PaymentService())->get_yiju_qingfee($item['id'], 'labourshen');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_labour_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','=',1];
        $where[] = ['jie.enginid','=',$param['enginid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|cai.gonger|jie.number|jie.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jie.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['koufee']) && !empty($param['koufee'])){
            $where[] = ['jie.koufee','like', '%'.$param['koufee'].'%'];
        }
        if(isset($param['fafee']) && !empty($param['fafee'])){
            $where[] = ['jie.fafee','like', '%'.$param['fafee'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jie.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }

        $data['data'] = LabourJieModel::alias('jie')
            ->field('jie.*,e.name as enginname,cai.name as cainame,cai.gonger')
            ->join('engin e','e.id=jie.enginid')
            ->join('contract_cai cai','cai.id=jie.caiid')
            ->where($where)
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                    ->where('status','in',[0,1])->find();
                if($deal){
                    $item['banknum'] = $deal['banknum'];
                    $item['bankname'] = $deal['bankname'];
                }

                $item['qingfee'] = (new PaymentService())->get_yiju_qingfee($item['id'], 'labourjie');
                $item['keshenfee'] = round($item['money'] - $item['qingfee'], 2);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_labour_yong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];
        $where[] = ['ling.isturn','=',1];
        $where[] = ['ling.status','=',1];
        $where[] = ['ling.enginid','=',$param['enginid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|ling.shi|ling.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ling.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ling.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shi']) && !empty($param['shi'])){
            $where[] = ['ling.shi','like', '%'.$param['shi'].'%'];
        }
        if(isset($param['yongxing']) && !empty($param['yongxing'])){
            $where[] = ['ling.yongxing','=', $param['yongxing']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }


        $data['data'] = LabourYongModel::alias('ling')
            ->field('ling.*,e.name as enginname')
            ->join('engin e','e.id=ling.enginid')
            ->where($where)
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caiid_name'] = '';
                $item['banknum'] = '';
                $item['bankname'] = '';

                if($item['yongxing'] == '劳务分包'){
                    $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
                    $item['gonger'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('gonger');
                    $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                        ->where('status','in',[0,1])->find();
                    if($deal){
                        $item['banknum'] = $deal['banknum'];
                        $item['bankname'] = $deal['bankname'];
                    }
                }

                $item['qingfee'] = (new PaymentService())->get_yiju_qingfee($item['id'], 'labouryong');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_yong');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_labour_ling(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];
        $where[] = ['ling.isturn','=',1];
        $where[] = ['ling.status','=',1];
        $where[] = ['ling.enginid','=',$param['enginid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['shen.number|shen.money|shen.baner|ling.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ling.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['ling.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['ling.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['ling.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ling.baner','like', '%'.$param['baner'].'%'];
        }



        $data['data'] = LabourLingModel::alias('ling')
            ->field('ling.*,e.name as enginname')
            ->join('engin e','e.id=ling.enginid')
            ->where($where)
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                    ->where('status','in',[0,1])->find();
                if($deal){
                    $item['banknum'] = $deal['banknum'];
                    $item['bankname'] = $deal['bankname'];
                }
                $item['qingfee'] = (new PaymentService())->get_yiju_qingfee($item['id'], 'labourling');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_labour_gong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ling.name|ling.idcard|ling.tel|ban.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['ling.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['idcard']) && !empty($param['idcard'])){
            $where[] = ['ling.idcard','like', '%'.$param['idcard'].'%'];
        }
        if(isset($param['tel']) && !empty($param['tel'])){
            $where[] = ['ling.tel','like', '%'.$param['tel'].'%'];
        }
        if(isset($param['yongxing']) && !empty($param['yongxing'])){
            $where[] = ['ling.yongxing','=', $param['yongxing']];
        }
        if(isset($param['gongtype']) && !empty($param['gongtype'])){
            $where[] = ['ling.gongtype','=', $param['gongtype']];
        }
        if(isset($param['banid_name']) && !empty($param['banid_name'])){
            $where[] = ['ban.name','like', '%'.$param['banid_name'].'%'];
        }
        if(isset($param['gonglei']) && !empty($param['gonglei'])){
            $where[] = ['ling.gonglei','=', $param['gonglei']];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['ling.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['ling.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['ling.banknum','like', '%'.$param['banknum'].'%'];
        }

        $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
        $data['is_yongxing'] = isset($param['yongxing']) ? $param['yongxing'] : '';

        $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
        $data['is_gongtype'] = isset($param['gongtype']) ? $param['gongtype'] : '';

        $data['gonglei'] = (new LabourService())->get_labour_gong_gonglei();
        $data['is_gonglei'] = isset($param['gonglei']) ? $param['gonglei'] : '';


        $data['data'] = LabourGongModel::alias('ling')
            ->field('ling.*,ban.name as banid_name')
            ->leftJoin('labour_ban ban','ban.id=ling.banid')
            ->where($where)
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                $item['gongtype'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_labour_xiang(Request $request){
        $param = $request->post();
        $xiang = LabourXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$param['enginid']])->find();
        if(!$xiang){
            return outputJson(2000,lang('code_2000'),[]);
        }

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];
        $where[] = ['ling.id','in',$xiang['gonger']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ling.name|ling.idcard|ling.tel','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['ling.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['idcard']) && !empty($param['idcard'])){
            $where[] = ['ling.idcard','like', '%'.$param['idcard'].'%'];
        }
        if(isset($param['tel']) && !empty($param['tel'])){
            $where[] = ['ling.tel','like', '%'.$param['tel'].'%'];
        }
        if(isset($param['yongxing']) && !empty($param['yongxing'])){
            $where[] = ['ling.yongxing','=', $param['yongxing']];
        }
        if(isset($param['gongtype']) && !empty($param['gongtype'])){
            $where[] = ['ling.gongtype','=', $param['gongtype']];
        }
        if(isset($param['banid']) && !empty($param['banid'])){
            $where[] = ['ling.banid','=', $param['banid']];
        }

        $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
        $data['is_yongxing'] = isset($param['yongxing']) ? $param['yongxing'] : '';

        $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
        $data['is_gongtype'] = isset($param['gongtype']) ? $param['gongtype'] : '';

        $enginid_name = EnginModel::where(['id'=>$xiang['enginid']])->value('name');
        $data['enginid_name'] = $enginid_name;

        $data['data'] = LabourGongModel::alias('ling')
            ->field('ling.*,ban.name as banid_name')
            ->leftJoin('labour_ban ban','ban.id=ling.banid')
            ->where($where)
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($enginid_name){
                $item['gongtype'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);
                $item['banid_name'] = LabourBanModel::where(['id'=>$item['banid']])->value('name');
                $item['enginid_name'] = $enginid_name;
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_labour_ban(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ling.name|ling.username|ling.usertel','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['ling.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['username']) && !empty($param['username'])){
            $where[] = ['ling.username','like', '%'.$param['username'].'%'];
        }
        if(isset($param['usertel']) && !empty($param['usertel'])){
            $where[] = ['ling.usertel','like', '%'.$param['usertel'].'%'];
        }

        $data['data'] = LabourBanModel::alias('ling')
            ->field('ling.*')
            ->where($where)
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $banname = LabourGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'banid'=>$item['id']])->column('name');
                $item['bannum'] = count($banname);
                $item['banname'] = implode('/',$banname);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


    public function get_subbao_shen(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shen.deleted_user','=', 0];
        $where[] = ['shen.comid','=',$this->userinfo['comid']];
        $where[] = ['shen.isturn','=',1];
        $where[] = ['shen.status','=',1];
        $where[] = ['shen.enginid','=',$param['enginid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.name|cai.number|shen.number|shen.baner|cai.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shen.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shenmoney']) && !empty($param['shenmoney'])){
            $where[] = ['shen.shenmoney','like', '%'.$param['shenmoney'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['shen.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shen.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['shen.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }


        $data['data'] = SubbaoShenModel::alias('shen')
            ->field('shen.*,e.name as enginname,cai.name as cainame,cai.gonger')
            ->join('engin e','e.id=shen.enginid')
            ->join('contract_cai cai','cai.id=shen.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('subbao_shen', 'shen'))
            ->order('shen.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                    ->where('status','in',[0,1])->find();
                if($deal){
                    $item['banknum'] = $deal['banknum'];
                    $item['bankname'] = $deal['bankname'];
                }
                $item['qingfee'] = (new PaymentService())->get_yiju_qingfee($item['id'], 'subbaoshen');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_subbao_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','=',1];
        $where[] = ['jie.enginid','=',$param['enginid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|cai.gonger|jie.number|jie.money|jie.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jie.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['koufee']) && !empty($param['koufee'])){
            $where[] = ['jie.koufee','like', '%'.$param['koufee'].'%'];
        }
        if(isset($param['fafee']) && !empty($param['fafee'])){
            $where[] = ['jie.fafee','like', '%'.$param['fafee'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jie.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['jie.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jie.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }


        $data['data'] = SubbaoJieModel::alias('jie')
            ->field('jie.*,e.name as enginname,cai.name as cainame,cai.gonger')
            ->join('engin e','e.id=jie.enginid')
            ->join('contract_cai cai','cai.id=jie.caiid')
            ->where($where)
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                    ->where('status','in',[0,1])->find();
                if($deal){
                    $item['banknum'] = $deal['banknum'];
                    $item['bankname'] = $deal['bankname'];
                }

                $item['qingfee'] = (new PaymentService())->get_yiju_qingfee($item['id'], 'subbaojie');
                $item['keshenfee'] = round($item['money'] - $item['qingfee'], 2);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_stuff_cai_pagepayment(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];
        $where[] = ['cai.enginid','=',$param['enginid']];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['cai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.number|cai.name|cai.number|cai.money|cai.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }

        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['type']) && $param['type'] != ''){
            $where[] = ['cai.type','=', $param['type']];
        }

        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['cai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['caidt']) && !empty($param['caidt'])){
            $where[] = ['cai.caidt','between', explode(' - ', $param['caidt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['cai.baner','like', '%'.$param['baner'].'%'];
        }



        $whereGonger = '';
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $whereGonger = "(cai.caiid > 0 and cai.gonger like '%".$param['gonger']."%')";
            $whereGonger .= " or (cai.caiid < 1 and cai.gonger like '%".$param['gonger']."%')";
        }

        $data['data'] = StuffCaiModel::alias('cai')
            ->field('cai.*,e.name as enginname,caihe.name as caiid_name,caihe.gonger as caigonger')
            ->join('engin e','e.id=cai.enginid')
            ->leftJoin('contract_cai caihe','caihe.id=cai.caiid')
            ->where($where)
            ->where($whereGonger)
            ->order('cai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['caiid'] > 0){
                    $item['gonger'] = $item['caigonger'];
                }
                $item['qingfee'] = (new PaymentService())->get_yiju_qingfee($item['id'], 'stuffcai');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_stuff_cai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];
        $where[] = ['cai.enginid','=',$param['enginid']];


        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['cai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.number|cai.name|cai.number|cai.money|cai.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }

        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['type']) && $param['type'] != ''){
            $where[] = ['cai.type','=', $param['type']];
        }

        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['cai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['caidt']) && !empty($param['caidt'])){
            $where[] = ['cai.caidt','between', explode(' - ', $param['caidt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['cai.baner','like', '%'.$param['baner'].'%'];
        }



        $whereGonger = '';
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $whereGonger = "(cai.caiid > 0 and cai.gonger like '%".$param['gonger']."%')";
            $whereGonger .= " or (cai.caiid < 1 and cai.gonger like '%".$param['gonger']."%')";
        }

        $stuffCai = StuffCaiModel::alias('cai')
            ->field('cai.*,e.name as enginname,caihe.name as caiid_name,caihe.gonger as caigonger')
            ->join('engin e','e.id=cai.enginid')
            ->leftJoin('contract_cai caihe','caihe.id=cai.caiid')
            ->where($where)
            ->where($whereGonger)
            ->order('cai.id DESC')
           ->select()->toArray();

        $info = [];
        foreach ($stuffCai as &$item){
            $henum = 0;
            $runum = 0;
            $list = ContractCaiListModel::field('id,num')->where(['caiid'=>$item['id']])->select()->toArray();
            foreach ($list as $vvv){
                $henum += $vvv['num'];
                $runum += StuffRuListModel::alias('list')
                    ->join('stuff_ru ff','ff.id=list.ruid')
                    ->where(['ff.comid'=>$this->userinfo['comid'],'ff.deleted_user'=>0])
                    ->where(['ff.isturn'=>1,'ff.status'=>1])
                    ->where(['ff.ishe'=>2])
                    ->where(['list.listid'=>$vvv['id']])
                    ->sum('num');
            }


            if($runum < $henum){
                if($item['caiid'] > 0){
                    $item['gonger'] = $item['caigonger'];
                }

                $item['banknum'] = '';
                $item['bankname'] = '';
                $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                    ->where('status','in',[0,1])->find();
                if($deal){
                    $item['banknum'] = $deal['banknum'];
                    $item['bankname'] = $deal['bankname'];
                }

                $info[] = $item;
            }
        }
        $data['data'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_stuff_cai_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=', 1];

        if(isset($param['caiid']) && $param['caiid'] != ''){
            $where[] = ['cai.id','=', $param['caiid']];
        }
        if(isset($param['enginid']) && $param['enginid'] != ''){
            $where[] = ['cai.enginid','=', $param['enginid']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['list.name|cai.number|cai.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['type']) && $param['type'] != ''){
            $where[] = ['cai.type','=', $param['type']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['cai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['cai.baner','like', '%'.$param['baner'].'%'];
        }

        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listunit']) && !empty($param['listunit'])){
            $where[] = ['list.unit','like', '%'.$param['listunit'].'%'];
        }
        if(isset($param['listprice']) && !empty($param['listprice'])){
            $where[] = ['list.price','like', '%'.$param['listprice'].'%'];
        }

        $data['data'] = StuffCaiListModel::alias('list')
            ->field('cai.*,e.name as enginname,list.id as listid,list.name as listname,list.unit as listunit
            ,list.price as listprice,list.num as listnum,list.money as listmoney')
            ->join('stuff_cai cai','cai.id=list.caiid')
            ->join('engin e','e.id=cai.enginid')
            ->where($where)
            ->order('cai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');

                $item['hanghao'] = $key+1;
                $item['runum'] =  StuffRuListModel::alias('list')
                    ->join('stuff_ru ru','ru.id=list.ruid')
                    ->where(['ru.deleted_user'=>0,'ru.isturn'=>1])
                    ->where('ru.status','in',[0,1])
                    ->where('ru.ishe','=',2)
                    ->where('list.listid','=',$item['listid'])
                    ->sum('list.num');
                $item['sheng_runum'] = round($item['listnum'] - $item['runum']);
                $item['rumoney'] =  StuffRuListModel::alias('list')
                    ->join('stuff_ru ru','ru.id=list.ruid')
                    ->where(['ru.deleted_user'=>0,'ru.isturn'=>1])
                    ->where('ru.ishe','=',1)
                    ->where('ru.status','in',[0,2])
                    ->where('list.listid','=',$item['listid'])
                    ->sum('list.money');

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_stuff_ru(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=',1];
        $where[] = ['ru.enginid','=',$param['enginid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.name|cai.number|ru.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }

        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ru.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['type']) && $param['type'] != ''){
            $where[] = ['ru.type','=', $param['type']];
        }

        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['ru.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['rudt']) && !empty($param['rudt'])){
            $where[] = ['ru.rudt','between', explode(' - ', $param['rudt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ru.baner','like', '%'.$param['baner'].'%'];
        }



        $data['data'] = StuffRuModel::alias('ru')
            ->field('ru.*,e.name as enginname,cai.name as caiid_name,cai.gonger as caigonger')
            ->join('engin e','e.id=ru.enginid')
            ->leftJoin('contract_cai cai','cai.id=ru.caiid')
            ->where($where)
            ->order('ru.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['caiid'] > 0){
                    $item['gonger'] = $item['caigonger'];
                }

                $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                    ->where('status','in',[0,1])->find();
                if($deal){
                    $item['banknum'] = $deal['banknum'];
                    $item['bankname'] = $deal['bankname'];
                }
                $item['qingfee'] = (new PaymentService())->get_yiju_qingfee($item['id'], 'stuffru');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_stuff_ru_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];
        $where[] = ['ru.enginid','=', $param['enginid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['list.name|ru.number|ru.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ru.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['type']) && $param['type'] != ''){
            $where[] = ['ru.type','=', $param['type']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['ru.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['ru.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['rudt']) && !empty($param['rudt'])){
            $where[] = ['ru.rudt','between', explode(' - ', $param['rudt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ru.baner','like', '%'.$param['baner'].'%'];
        }

        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listunit']) && !empty($param['listunit'])){
            $where[] = ['list.unit','like', '%'.$param['listunit'].'%'];
        }
        if(isset($param['listprice']) && !empty($param['listprice'])){
            $where[] = ['list.price','like', '%'.$param['listprice'].'%'];
        }

        $list = StuffRuListModel::alias('list')
            ->field('ru.*,e.name as enginname,list.id as listid,list.name as listname,list.unit as listunit
            ,list.price as listprice,list.num as listnum,list.money as listmoney')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->where($where)
            ->order('ru.id DESC')
            ->select()->toArray();



        $isku = isset($param['isku']) ? $param['isku'] : 0;


        $info = [];
        foreach ($list as $key=>&$item){
            $item['hanghao'] = $key+1;
            $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
            $item['chunum'] = StuffChuModel::where(['enginid'=>$item['enginid'],'ruid'=>$item['listid']])->sum('num');
            $item['tuinum'] = StuffTuiModel::where(['enginid'=>$item['enginid'],'ruid'=>$item['listid']])->sum('num');
            $item['cunnum'] = $item['listnum'] - $item['chunum'] + $item['tuinum'];
            $item['cunmoney'] = $item['cunnum'] * $item['listprice'];


            if($isku == 1){
                if($item['cunnum'] > 0){
                    $info[] = $item;
                }
            }else if($isku == 2){
                if($item['cunnum'] <= 0){
                    $info[] = $item;
                }
            }else{
                $info[] = $item;
            }
        }


        $data['data'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_stuff_ru_list_pagejie(Request $request){
        $param = $request->post();

        $where[] = ['enginid','=',$param['enginid']];
        $where[] = ['deleted_user','=',0];
        $where[] = ['isturn','=',1];
        $where[] = ['status','=',1];

        if(isset($param['caiid']) && $param['caiid'] > 0){
            $where[] = ['caiid','=',$param['caiid']];
            $ruidArr = StuffRuModel::where($where)->column('id');
        }else{
            $where[] = ['gonger','=',$param['gonger']];
            $ruidArr = StuffRuModel::where($where)->column('id');
        }


        $list = StuffRuListModel::alias('list')
            ->field('list.*,ru.rudt as dt')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->where('list.ruid','in',$ruidArr)
            ->select()->toArray();

        $info = [];
        foreach ($list as $item){
            $exist =  StuffJieListModel::alias('listjie')
                ->join('stuff_jie jie','jie.id=listjie.jieid')
                ->where(['jie.deleted_user'=>0,'jie.isturn'=>1])
                ->where('jie.status','in',[0,1])
                ->where('listjie.listid','=',$item['id'])
                ->find();
            if(!$exist){
                $info[] = $item;
            }
        }

        $data['data'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_stuff_chu_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['enginid','=',$param['enginid']];
        $where[] = ['deleted_user','=',0];
        $where[] = ['isturn','=',1];
        $where[] = ['status','=',1];

        $ruidArr = StuffChuModel::where($where)->column('id');

        $data['data'] = StuffChuListModel::where('chuid','in',$ruidArr)
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_stuff_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','=',1];
        $where[] = ['jie.enginid','=',$param['enginid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.name|jie.number|jie.gonger|jie.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['jie.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jie.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['jiedt']) && !empty($param['jiedt'])){
            $where[] = ['jie.jiedt','between', explode(' - ', $param['jiedt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jie.baner','like', '%'.$param['baner'].'%'];
        }



        $data['data'] = StuffJieModel::alias('jie')
            ->field('jie.*,e.name as enginname')
            ->join('engin e','e.id=jie.enginid')
            ->leftJoin('contract_cai cai','cai.id=jie.caiid')
            ->where($where)
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');

                $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                    ->where('status','in',[0,1])->find();
                if($deal){
                    $item['banknum'] = $deal['banknum'];
                    $item['bankname'] = $deal['bankname'];
                }
                $item['qingfee'] = (new PaymentService())->get_yiju_qingfee($item['id'], 'stuffjie');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }


    public function get_speed_zong_list(Request $request){
        $param = $request->post();
        get_page($param);
        $zong = SpeedZongModel::where(['deleted_user'=>0,'enginid'=>$param['enginid']])->find();

        $where[] = ['zongid','=',$zong['id']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['code|buname|xiangname|unit|baner','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['code']) && !empty($param['code'])){
            $where[] = ['code','like', '%'.$param['code'].'%'];
        }
        if(isset($param['buname']) && !empty($param['buname'])){
            $where[] = ['buname','like', '%'.$param['buname'].'%'];
        }
        if(isset($param['xiangname']) && !empty($param['xiangname'])){
            $where[] = ['xiangname','like', '%'.$param['xiangname'].'%'];
        }
        if(isset($param['unit']) && !empty($param['unit'])){
            $where[] = ['unit','like', '%'.$param['unit'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['baner','like', '%'.$param['baner'].'%'];
        }
        $data = SpeedZongListModel::where($where)->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_speed_zong_list_page_speedmonth(Request $request){
        $param = $request->post();
        get_page($param);
        $zong = SpeedZongModel::where(['deleted_user'=>0,'enginid'=>$param['enginid']])->find();

        $where[] = ['zongid','=',$zong['id']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['code|buname|xiangname|unit|baner','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['code']) && !empty($param['code'])){
            $where[] = ['code','like', '%'.$param['code'].'%'];
        }
        if(isset($param['buname']) && !empty($param['buname'])){
            $where[] = ['buname','like', '%'.$param['buname'].'%'];
        }
        if(isset($param['xiangname']) && !empty($param['xiangname'])){
            $where[] = ['xiangname','like', '%'.$param['xiangname'].'%'];
        }
        if(isset($param['unit']) && !empty($param['unit'])){
            $where[] = ['unit','like', '%'.$param['unit'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['baner','like', '%'.$param['baner'].'%'];
        }
        $data = SpeedZongListModel::where($where)->select()->toArray();

        foreach ($data as &$item){
            $item['shifindt'] = EnginSpeedListModel::alias('list')
                ->join('engin_speed speed','speed.id=list.speedid')
                ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                ->where(['speed.enginid'=>$param['enginid'],'list.zongid'=>$item['id']])
                ->value('findt');

            $item['leishinum'] = EnginSpeedListModel::alias('list')
                ->join('engin_speed speed','speed.id=list.speedid')
                ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                ->where(['speed.enginid'=>$param['enginid'],'list.zongid'=>$item['id']])
                ->sum('shinum');

            $item['leishichan'] = round($item['leishinum']*$item['price'], 2);
            $item['leishiday'] = EnginSpeedListModel::alias('list')
                ->join('engin_speed speed','speed.id=list.speedid')
                ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                ->where(['speed.enginid'=>$param['enginid'],'list.zongid'=>$item['id']])
                ->sum('shiday');

            $item['dangjin'] = 0;
            if($item['num'] != 0){
                $item['dangjin'] = round($item['leishinum']/$item['num']*100, 2);
            }

            $item['gongbi'] = 0;
            if($item['duration'] != 0){
                $item['gongbi'] = round($item['leishiday']/$item['duration']*100, 2);
            }

            $item['gongjing'] = false;
            if($item['dangjin'] >= $item['gongbi']){
                $item['gongjing'] = true;
            }

            $item['dainum'] = $item['num']-$item['leishinum'];
            $item['daichan'] = $item['chanfee']-$item['leishichan'];
            $item['sheng'] = $item['duration']-$item['leishiday'];

            $item['zuijin'] = EnginSpeedListModel::alias('list')
                ->join('engin_speed speed','speed.id=list.speedid')
                ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                ->where(['speed.enginid'=>$param['enginid'],'list.zongid'=>$item['id']])
                ->order('speed.month DESC')
                ->value('speed.month');

        }

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_speed_month(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['month.deleted_user','=', 0];
        $where[] = ['month.comid','=',$this->userinfo['comid']];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['month.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|month.number|month.name|month.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['month.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['month.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $where[] = ['month.month','=', $param['month']];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['month.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['month.dt','between', explode(' - ', $param['dt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = SpeedMonthModel::alias('month')
            ->field('month.*,e.name as enginname')
            ->join('engin e','e.id=month.enginid')
            ->where($where)
            ->order('month.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_speed_month_list(Request $request){
        $param = $request->post();
        get_page($param);
        $info = SpeedMonthModel::where(['deleted_user'=>0,'id'=>$param['monthid']])->find();
        $where[] = ['monthid','=',$info['id']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['code|buname|xiangname|unit','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['code']) && !empty($param['code'])){
            $where[] = ['code','like', '%'.$param['code'].'%'];
        }
        if(isset($param['buname']) && !empty($param['buname'])){
            $where[] = ['buname','like', '%'.$param['buname'].'%'];
        }
        if(isset($param['xiangname']) && !empty($param['xiangname'])){
            $where[] = ['xiangname','like', '%'.$param['xiangname'].'%'];
        }
        if(isset($param['unit']) && !empty($param['unit'])){
            $where[] = ['unit','like', '%'.$param['unit'].'%'];
        }

        $data = SpeedMonthListModel::where($where)->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function get_device_jie(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','=',1];
        $where[] = ['jie.enginid','=',$param['enginid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.name|cai.number|cai.gonger|jie.number|jie.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['caigonger']) && !empty($param['caigonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['caigonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jie.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['jie.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jie.baner','like', '%'.$param['baner'].'%'];
        }



        $data['data'] = DeviceJieModel::alias('jie')
            ->field('jie.*,e.name as enginname,cai.name as cainame,cai.number as cainumber,cai.gonger as caigonger')
            ->join('engin e','e.id=jie.enginid')
            ->join('contract_cai cai','cai.id=jie.caiid')
            ->where($where)
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['gonger'] = $item['caigonger'];
                $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                    ->where('status','in',[0,1])->find();
                if($deal){
                    $item['banknum'] = $deal['banknum'];
                    $item['bankname'] = $deal['bankname'];
                }
                $item['qingfee'] = (new PaymentService())->get_yiju_qingfee($item['id'], 'devicejie');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_device_lin(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['lin.deleted_user','=', 0];
        $where[] = ['lin.comid','=',$this->userinfo['comid']];
        $where[] = ['lin.isturn','=',1];
        $where[] = ['lin.status','=',1];
        $where[] = ['lin.enginid','=',$param['enginid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['lin.gonger|lin.number|lin.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['lin.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['lin.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['lin.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['lin.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['lin.baner','like', '%'.$param['baner'].'%'];
        }



        $data['data'] = DeviceLinModel::alias('lin')
            ->field('lin.*,e.name as enginname')
            ->join('engin e','e.id=lin.enginid')
            ->where($where)
            ->order('lin.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $deal = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'name'=>$item['gonger']])
                    ->where('status','in',[0,1])->find();
                if($deal){
                    $item['banknum'] = $deal['banknum'];
                    $item['bankname'] = $deal['bankname'];
                }
                $item['qingfee'] = (new PaymentService())->get_yiju_qingfee($item['id'], 'devicelin');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_device_jin_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jin.deleted_user','=', 0];
        $where[] = ['jin.comid','=',$this->userinfo['comid']];
        $where[] = ['jin.isturn','=',1];
        $where[] = ['jin.status','=', 1];
        $where[] = ['jin.caiid','=', $param['caiid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['list.name|list.shenum|cai.name|cai.number|cai.gonger|jin.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['caigonger']) && !empty($param['caigonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['caigonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jin.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['jin.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jin.baner','like', '%'.$param['baner'].'%'];
        }

        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listunit']) && !empty($param['listunit'])){
            $where[] = ['list.unit','like', '%'.$param['listunit'].'%'];
        }
        if(isset($param['listprice']) && !empty($param['listprice'])){
            $where[] = ['list.price','like', '%'.$param['listprice'].'%'];
        }
        if(isset($param['listdigit']) && !empty($param['listdigit'])){
            $where[] = ['list.digit','like', '%'.$param['listdigit'].'%'];
        }
        if(isset($param['listjindt']) && !empty($param['listjindt'])){
            $where[] = ['list.jindt','between', explode(' - ', $param['listjindt'])];
        }
        if(isset($param['listshenum']) && !empty($param['listshenum'])){
            $where[] = ['list.shenum','like', '%'.$param['listshenum'].'%'];
        }


        $data['data'] = DeviceJinListModel::alias('list')
            ->field('jin.*,e.name as enginname,cai.name as cainame,cai.gonger,list.id as listid,list.name as listname
            ,list.unit as listunit,list.price as listprice,list.digit as listdigit,list.jindt as listjindt,
            list.shenum as listshenum')
            ->join('device_jin jin','jin.id=list.jinid')
            ->join('engin e','e.id=jin.enginid')
            ->join('contract_cai cai','cai.id=jin.caiid')
            ->where($where)
            ->order('jin.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['status_name'] = '在场';
                $item['tuidt'] = '';
                $tui = DeviceTuiModel::where(['enginid'=>$item['enginid'],'caiid'=>$item['caiid'],'deleted_user'=>0,'jinid'=>$item['listid']])->find();
                if($tui){
                    $item['status_name'] = '退场';
                    $item['tuidt'] = $tui['tuidt'];
                    $item['days'] = count_days($item['listjindt'], $tui['tuidt']);
                }else{
                    $item['days'] = count_days($item['listjindt'], date("Y-m-d"));
                }

                $item['yununit'] = DeviceYunModel::where(['enginid'=>$item['enginid'],'caiid'=>$item['caiid'],'deleted_user'=>0,'jinid'=>$item['listid']])->sum('yununit');

                $item['leijieunit'] =  DeviceJieListModel::alias('list')
                    ->join('device_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1])
                    ->where('jie.status','in',[0,1])
                    ->where('list.listid','=',$item['listid'])
                    ->sum('jieunit');
                $item['leijiemoney'] =  DeviceJieListModel::alias('list')
                    ->join('device_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1])
                    ->where('jie.status','in',[0,1])
                    ->where('list.listid','=',$item['listid'])
                    ->sum('list.money');
                $item['daijieunit'] = $item['yununit'] - $item['leijieunit'];

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_device_jin_list_page_devicetui(Request $request){
        $param = $request->post();

        $where[] = ['jin.deleted_user','=', 0];
        $where[] = ['jin.comid','=',$this->userinfo['comid']];
        $where[] = ['jin.isturn','=',1];
        $where[] = ['jin.status','=', 1];
        $where[] = ['jin.caiid','=', $param['caiid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|cai.name|cai.number|cai.gonger|jin.number|list.name|list.unit|list.price|list.digit|list.shenum','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['caigonger']) && !empty($param['caigonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['caigonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jin.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['jin.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jin.baner','like', '%'.$param['baner'].'%'];
        }

        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listunit']) && !empty($param['listunit'])){
            $where[] = ['list.unit','like', '%'.$param['listunit'].'%'];
        }
        if(isset($param['listprice']) && !empty($param['listprice'])){
            $where[] = ['list.price','like', '%'.$param['listprice'].'%'];
        }
        if(isset($param['listdigit']) && !empty($param['listdigit'])){
            $where[] = ['list.digit','like', '%'.$param['listdigit'].'%'];
        }
        if(isset($param['listjindt']) && !empty($param['listjindt'])){
            $where[] = ['list.jindt','between', explode(' - ', $param['listjindt'])];
        }
        if(isset($param['listshenum']) && !empty($param['listshenum'])){
            $where[] = ['list.shenum','like', '%'.$param['listshenum'].'%'];
        }


         $info = [];
         $list = DeviceJinListModel::alias('list')
            ->field('jin.*,e.name as enginname,cai.name as cainame,cai.gonger,list.id as listid,list.name as listname
            ,list.unit as listunit,list.price as listprice,list.digit as listdigit,list.jindt as listjindt,
            list.shenum as listshenum')
            ->join('device_jin jin','jin.id=list.jinid')
            ->join('engin e','e.id=jin.enginid')
            ->join('contract_cai cai','cai.id=jin.caiid')
            ->where($where)
            ->order('jin.id DESC')
            ->select()->toArray();

         foreach ($list as $item){
             $item['status_name'] = '在场';
             $tui = DeviceTuiModel::where(['enginid'=>$item['enginid'],'caiid'=>$item['caiid'],'deleted_user'=>0,'jinid'=>$item['listid']])->find();
             if(!$tui){
                 $item['yununit'] = $yununit = DeviceYunModel::where(['enginid'=>$item['enginid'],'caiid'=>$item['caiid'],'deleted_user'=>0,'jinid'=>$item['listid']])->sum('yununit');
                 $info[] = $item;
             }
         }



        $data['data'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function get_flowrelas(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
//        $where[] = ['bill.created_user','=',$this->userinfo['id']];
        $where[] = ['bill.isturn','=', 1];
        $where[] = ['bill.status','=', 1];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|bill.summary|bill.enginame','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['bill.enginame','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }



        $datatype = $request->param('datatype','wofa','string');
        //我发起的
        if($datatype == 'wofa'){
            $where[] = ['bill.created_user','=',$this->userinfo['id']];
            $data['data'] = FlowBillModel::alias('bill')
                ->field('bill.*,e.name as enginname')
                ->leftJoin('engin e','e.id=bill.enginid')
                ->where($where)
                ->order('bill.id DESC')
                ->paginate($param['pagesize'],false,['page'=>$param['page']])
                ->each(function ($item,$key){
                    return $item;
                })->toArray();
        }

        //我审核的
        if($datatype == 'woshen'){
            $where[] = ['shen.uid','=', $this->userinfo['id']];
            $data['data'] = FlowTaskShenModel::alias('shen')
                ->field('bill.*,e.name as enginname')
                ->join('flow_bill bill','bill.id=shen.billid')
                ->leftJoin('engin e','e.id=bill.enginid')
                ->where($where)
                ->group('bill.id')
                ->paginate($param['pagesize'],false,['page'=>$param['page']])
                ->each(function ($item,$key){
                    return $item;
                })->toArray();
        }

        //抄送我的
        if($datatype == 'chaowo'){
            $wherePromer = Db::raw("promer.uid=".$this->userinfo['id']." or find_in_set(".$this->userinfo['id'].",domer)");
            $data['data'] = FlowPromerModel::alias('promer')
                ->field('bill.*,e.name as enginname')
                ->join('flow_bill bill','promer.billid = bill.id')
                ->leftJoin('engin e','e.id=bill.enginid')
                ->where($where)
                ->where($wherePromer)
                ->group('bill.id')
                ->paginate($param['pagesize'],false,['page'=>$param['page']])
                ->each(function ($item,$key){
                    return $item;
                })->toArray();
        }

        //分享我的
        if($datatype == 'fenwo'){
            $whereShare = Db::raw("share.uid=".$this->userinfo['id']." or find_in_set(".$this->userinfo['id'].",domer)");
            $data['data'] = FlowShareModel::alias('share')
                ->field('bill.*,e.name as enginname')
                ->leftJoin('flow_bill bill','share.billid = bill.id')
                ->leftJoin('engin e','e.id=bill.enginid')
                ->where($where)
                ->where($whereShare)
                ->group('bill.id')
                ->paginate($param['pagesize'],false,['page'=>$param['page']])
                ->each(function ($item,$key){
                    return $item;
                })->toArray();
        }


//        $data['data'] = FlowBillModel::alias('bill')
//            ->field('bill.*')
//            ->where($where)
//            ->order('bill.id DESC')
//            ->paginate($param['pagesize'],false,['page'=>$param['page']])
//            ->each(function ($item,$key){
////                $item['status_name'] = (new FlowService())->get_flow_flowtext($item);
//                return $item;
//            })->toArray();



        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        $data['is_modeid'] = isset($param['modeid']) ? $param['modeid'] : '';
        return outputJson(2000,lang('code_2000'),$data);
    }



    public function get_engin_budget(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bud.deleted_user','=', 0];
        $where[] = ['bud.comid','=',$this->userinfo['comid']];
        $where[] = ['bud.isturn','=',1];
        $where[] = ['bud.status','=',1];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bud.name|bud.bianer|bud.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['bud.enginid','=',$param['enginid']];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bud.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bud.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['bianer']) && !empty($param['bianer'])){
            $where[] = ['bud.bianer','like', '%'.$param['bianer'].'%'];
        }
        if(isset($param['biandt']) && !empty($param['biandt'])){
            $where[] = ['bud..biandt','between', explode(' - ', $param['biandt'])];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['bud.money','like', '%'.$param['money'].'%'];
        }


        $data['data'] = EnginBudgetModel::alias('bud')
            ->field('bud.*,e.name as enginname')
            ->join('engin e','e.id=bud.enginid')
            ->where($where)
            ->order('bud.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_budsub(Request $request){
        $nogong = $request->param('nogong','','string');
        $data['data'] = (new BudgetService())->get_engin_budsub_all_xiala(0,$nogong);
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_budsub_guan(Request $request){
        $param = $request->post();
        $topid = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0,'pid'=>0,'name'=>'项目费用'])->value('id');


        $name = '';
        if(isset($param['name']) && !empty($param['name'])){
            $name = $param['name'];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $name = $param['keyword'];
        }

        if($name){
            $list = EnginBudsubModel::field('id,pid,name')
                ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0])
                ->where("id = ".$topid." or topid=".$topid)
                ->where("name like '%".$name."%'")->column('id');


            $info = [];
            foreach($list as $vvv){
                $toparr = (new BudgetService())->get_budsub_tops($vvv);
                $sonarr = (new BudgetService())->get_budsub_sons($vvv);
                $info = array_unique(array_merge($info, $toparr, $sonarr));
            }

            $list = EnginBudsubModel::field('id,pid,name')->where('id','in',$info)->select()->toArray();


        }else{
            $list = EnginBudsubModel::field('id,pid,name')
                ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0])
                ->where("id = ".$topid." or topid=".$topid)
                ->select()->toArray();
        }


        $data = array2tree($list, 'id', 'pid', 'children');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_budsub_guan_gong(Request $request){
        $param = $request->post();
        $topid = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0,'pid'=>0,'name'=>'公司费用'])->value('id');

        $name = '';
        if(isset($param['name']) && !empty($param['name'])){
            $name = $param['name'];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $name = $param['keyword'];
        }

        if($name){
            $list = EnginBudsubModel::field('id,pid,name')
                ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0])
                ->where("id = ".$topid." or topid=".$topid)
                ->where("name like '%".$name."%'")->column('id');


            $info = [];
            foreach($list as $vvv){
                $toparr = (new BudgetService())->get_budsub_tops($vvv);
                $sonarr = (new BudgetService())->get_budsub_sons($vvv);
                $info = array_unique(array_merge($info, $toparr, $sonarr));
            }

            $list = EnginBudsubModel::field('id,pid,name')->where('id','in',$info)->select()->toArray();


        }else{
            $list = EnginBudsubModel::field('id,pid,name')
                ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0])
                ->where("id = ".$topid." or topid=".$topid)
                ->select()->toArray();
        }


        $data = array2tree($list, 'id', 'pid', 'children');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_budshou_engin(Request $request){
        $param = $request->post();
        $topid = EnginBudshouModel::where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0,'pid'=>0,'name'=>'项目收入'])->value('id');

        $name = '';
        if(isset($param['name']) && !empty($param['name'])){
            $name = $param['name'];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $name = $param['keyword'];
        }


        if($name){
            $list = EnginBudshouModel::field('id,pid,name,sort')
                ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0])
                ->where("id = ".$topid." or topid=".$topid)
                ->where("name like '%".$name."%'")->column('id');


            $info = [];
            foreach($list as $vvv){
                $toparr = (new BudgetService())->get_budshou_tops($vvv);
                $sonarr = (new BudgetService())->get_budshou_sons($vvv);
                $info = array_unique(array_merge($info, $toparr, $sonarr));
            }

            $list = EnginBudshouModel::field('id,pid,name,sort')->where('id','in',$info)->select()->toArray();


        }else{
            $list = EnginBudshouModel::field('id,pid,name,sort')
                ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0])
                ->where("id = ".$topid." or topid=".$topid)
                ->select()->toArray();
        }


        $data = array2tree($list, 'id', 'pid', 'children', 'sort');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_budshou_qi(Request $request){
        $param = $request->post();
        $topid = EnginBudshouModel::where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0,'pid'=>0,'name'=>'其他收入'])->value('id');


        $name = '';
        if(isset($param['name']) && !empty($param['name'])){
            $name = $param['name'];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $name = $param['keyword'];
        }

        if($name){
            $list = EnginBudshouModel::field('id,pid,name,sort')
                ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0])
                ->where("id = ".$topid." or topid=".$topid)
                ->where("name like '%".$name."%'")->column('id');


            $info = [];
            foreach($list as $vvv){
                $toparr = (new BudgetService())->get_budshou_tops($vvv);
                $sonarr = (new BudgetService())->get_budshou_sons($vvv);
                $info = array_unique(array_merge($info, $toparr, $sonarr));
            }

            $list = EnginBudshouModel::field('id,pid,name,sort')->where('id','in',$info)->select()->toArray();


        }else{
            $list = EnginBudshouModel::field('id,pid,name,sort')
                ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0])
                ->where("id = ".$topid." or topid=".$topid)
                ->select()->toArray();
        }


        $data = array2tree($list, 'id', 'pid', 'children','sort');
        return outputJson(2000,lang('code_2000'),$data);
    }





    public function get_engin_budget_list(Request $request){
        $param = $request->post();
        $budgetid = $request->param('budgetid', 0, 'int');


        $where[] = ['budid','=',$budgetid];
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['subid']) && !empty($param['subid'])){
            $where[] = ['subid','=', $param['subid']];
        }


        $info = EnginBudgetListModel::where($where)->select()->toArray();
        foreach ($info as &$vv){
            $vv['quanValue'] = (new BudgetService())->get_subid_quanname($vv['subid']);
            $vv['topname'] = EnginBudsubModel::where(['id'=>$vv['subid']])->value('name');
            $vv['zhannum'] = ContractCaiListModel::alias('list')
                ->join('contract_cai cai','cai.id = list.caiid')
                ->where(['cai.deleted_user'=>0,'cai.isturn'=>1])
                ->where('cai.status','in',[0,1])
                ->where(['list.listid'=>$vv['id']])
                ->sum('list.num');
            $vv['shengnum'] = $vv['num'] - $vv['zhannum'];

            $subidArr[] = $vv['subid'];
        }


        $data['data'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_budget_list222(Request $request){

        $budgetid = $request->param('budgetid', 0, 'int');

        $subidArr = [];
        $info = EnginBudgetListModel::where(['budid'=>$budgetid])->select()->toArray();
        foreach ($info as &$vv){
            $vv['quanValue'] = (new BudgetService())->get_subid_quanname($vv['subid']);
            $vv['subid_name'] = $vv['name'];
            $vv['name'] = '#';
            $vv['topname'] = EnginBudsubModel::where(['id'=>$vv['subid']])->value('name');
            $vv['zhannum'] = ContractCaiListModel::alias('list')
                ->join('contract_cai cai','cai.id = list.caiid')
                ->where(['cai.deleted_user'=>0,'cai.isturn'=>1])
                ->where('cai.status','in',[0,1])
                ->where(['list.listid'=>$vv['id']])
                ->sum('list.num');
            $vv['shengnum'] = $vv['num'] - $vv['zhannum'];

            $subidArr[] = $vv['subid'];
        }

        $subidArr = array_unique($subidArr);

        $zhongArr = $subidArr;
        foreach ($subidArr as $v){
            $arr = (new BudgetService())->get_shangji_dingji($v);
            $zhongArr = array_merge($zhongArr, $arr);
        }

        $list = EnginBudsubModel::where('id','in',$zhongArr)->select()->toArray();
        foreach ($list as &$item){
            $item['children'] = [];
            foreach ($info as $vvv){
                if($vvv['subid'] == $item['id']){
                    $item['children'][] = $vvv;
                }
            }
        }

        $data['data'] = array2tree($list, 'id', 'pid', 'children');
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_user_promeropt(Request $request){
        $param = $request->post();
        $bill = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$param['id']])->find();
        $domer = FlowPromerOptModel::where(['modenum'=>$bill['modenum'],'uid'=>$this->userinfo['id']])->value('domer');
        $data['promer_info'] = (new UserService())->get_users_part_arr($domer, 'user');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_flow_used(Request $request){
        $data = FlowUsedModel::where(['comid'=>$this->userinfo['comid'],'uid'=>$this->userinfo['id']])->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_flow_used(Request $request){
        $explain = $request->param('explain', '', 'string');

        if(empty($explain)){
            return outputJson(4000,lang('code_4000'));
        }

        FlowUsedModel::insertGetId([
            'comid' => $this->userinfo['comid'],
            'uid' => $this->userinfo['id'],
            'explain' => $explain,
        ]);

        $data = FlowUsedModel::where(['comid'=>$this->userinfo['comid'],'uid'=>$this->userinfo['id']])->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_flow_used(Request $request){
        $id = $request->param('id', 0, 'int');
        FlowUsedModel::where(['comid'=>$this->userinfo['comid'],'uid'=>$this->userinfo['id'],'id'=>$id])->delete();
        $data = FlowUsedModel::where(['comid'=>$this->userinfo['comid'],'uid'=>$this->userinfo['id']])->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }


    public function get_flow_tui_info(Request $request){
        $param = $request->post();
        $bill = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$param['id']])->find();
        if(!$bill){
            return outputJson(4000, "无法退回");
        }
        if($bill['isturn'] != 1){
            return outputJson(4000, "无法退回");
        }
        if($bill['status'] != 0){
            return outputJson(4000, "无法退回");
        }

        $minfo = Db::name($bill['modenum'])->where(['id'=>$bill['mid']])->find();
        if(!$minfo){
            return outputJson(4000, "无法退回");
        }

        $myTask = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id'],'state'=>1])
            ->where(['uid'=>$this->userinfo['id'],'deleted_user'=>0])->find();
        if(!$myTask){
            return outputJson(4000, "无法退回");
        }

        $node = FlowNodeModel::where(['id'=>$myTask['nodeid']])->find();
        if(!$node){
            return outputJson(4000, "无法退回");
        }

        $listJie = FlowNodeModel::field('id,name')->where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])
            ->where('id','lt', $node['id'])->order('id DESC')->select()->toArray();

        $listRen = [];
        foreach ($listJie as $k=>$v){
            $sontask = FlowTaskModel::field('id,inituid')->where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id'],'nodeid'=>$v['id'],'pid'=>0,'state'=>2])
                ->order('id DESC')->select()->toArray();
            if($sontask){
                foreach ($sontask as $kk=>$vv){
                    $inituid_name = UserModel::get_realname_by_pk($vv['inituid']);
                    $listRen[] = ['id'=>$vv['id'], 'name'=>$v['name'].'：'.$inituid_name];
                }
            }
        }

        $listJie[] = ['id'=>0, 'name'=>'申请人发起申请'];

        if($node['type'] == 2){
            $listJie = [['id'=>0, 'name'=>'申请人发起申请']];
        }

        $data['listJie'] = $listJie;
        $data['listRen'] = $listRen;
        $data['nodetype'] = $node['type'];
        return outputJson(2000,lang('code_2000'),$data);
    }



}





















